Skip to content

Instantly share code, notes, and snippets.

@scott-coates
Last active August 29, 2015 14:22
Show Gist options
  • Save scott-coates/d399d4c30608b23ca552 to your computer and use it in GitHub Desktop.
Save scott-coates/d399d4c30608b23ca552 to your computer and use it in GitHub Desktop.
A slightly biased comparison of ES7 async/await (a la C#) vs ES6 promises. The idea is to delay the return of data, simulating a a request/response from a server.
const mockAPI = {
provideResponse(dataToProvide) {
const timeout = Math.round(Math.random() * (3000 - 500)) + 500;
const retVal = new Promise((resolve, reject)=> {
setTimeout(() => {
resolve(dataToProvide);
}, timeout);
});
return retVal;
}
};
export default mockAPI;
import sleep from './sleep';
const mockAPI = {
async provideResponse(dataToProvide) {
const timeout = Math.round(Math.random() * (3000 - 500)) + 500;
await sleep(timeout);
return dataToProvide;
}
};
export default mockAPI;
//https://gist.github.com/danharper/74a5102363fbd85f6b67
export default function (ms = 0) {
return new Promise(r => setTimeout(r, ms));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment