Skip to content

Instantly share code, notes, and snippets.

@ivawzh
Last active January 30, 2016 06:24
Show Gist options
  • Save ivawzh/005b12e959478bd56f43 to your computer and use it in GitHub Desktop.
Save ivawzh/005b12e959478bd56f43 to your computer and use it in GitHub Desktop.
async/await of ES7 101
// Conclusion:
// Each synchronous call blocks Node's event loop. All concurrent tasks are blocked, and the event loop sits idle, until the call completes.
// Another important note, async/await runs at 79% of the speed of bare callbacks.
// Full documentation: https://github.com/yortus/asyncawait
import { describe, it } from 'mocha';
import { expect } from 'chai';
describe('async/await', ()=> {
it.only('try sync', async done => {
console.log('begin')
// create sleep function using Promise
function sleep(time) {
return new Promise((resolve, reject) => {
console.log('start to sleep')
return setTimeout(()=> {
console.log('Woke up! Slept for ' + time + ' ms')
return resolve()
// the following is equivalent as `return resolve()`, i.e. unnecessary to return resolve()/reject() from Promise
// resolve()
// return
}, time)
})
}
// NOTE: async function's `return result` produces `Promise.resolve(result)` behind the scene
// create a buggy sleep function using async
// this function is not working!!! Because `return setTimeout(...)` will become `Promise.resolve(setTimeout(...))`, which will trigger the next step immediately instead of wait for 1 sec.
//async function sleep(time) {
// console.log('start to sleep')
// return setTimeout(()=> {
// console.log('Woke up! Slept for ' + time + ' ms')
// return
// }, time)
//}
// async version of getValue function
async function obtainValue() {
console.log('start to obtain value')
await sleep(1000)
return 99
}
// Promise version of getValue function
function getValue() {
return new Promise((resolve, reject) => {
console.log('start to get value')
sleep(1000).then(()=>{
resolve(99)
}
)
})
}
async function produceError() {
console.log('start to produce error')
await sleep(1000)
throw new Error('error as expected')
}
function getError() {
console.log('start to get error')
await sleep(1000)
throw new Error('error as expected')
}
const value = await getValue()
console.log('Successfully got value: ', value)
const value2 = await obtainValue()
console.log('Successfully obtain value: ', value2)
console.log('Try to do something parallel before wake up from sleep. But it does not work. Looks like you cannot do sync function after await.')
try{
await produceError()
}
catch(err){
console.log('Successfully captured error: ' + err)
}
try{
console.log('Try to catch error from a group of async functions')
await getValue()
await produceError()
await obtainValue()
console.log('If this message occurs, it means the `error catch` does not stop the "chain" running. Thankfully this is not the case.')
}
catch(err){
console.log('Successfully captured error again: ' + err)
}
done()
})
});
/*
### mocha result:
begin
start to get value
start to sleep
Woke up! Slept for 1000 ms
Successfully got value: 99
start to obtain value
start to sleep
Woke up! Slept for 1000 ms
Successfully obtain value: 99
Try to do something parallel before wake up from sleep. But it does not work. Looks like you cannot do sync function after await.
start to produce error
start to sleep
Woke up! Slept for 1000 ms
Successfully captured error: Error: error as expected
Try to catch error from a group of async functions
start to get value
start to sleep
Woke up! Slept for 1000 ms
start to produce error
start to sleep
Woke up! Slept for 1000 ms
Successfully captured error again: Error: error as expected
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment