Last active
August 29, 2015 14:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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