Last active
June 3, 2017 09:51
-
-
Save gskachkov/d1322c29fe2f6ff3c3ba670f2a6f4ff2 to your computer and use it in GitHub Desktop.
Async function as plain function
This file contains hidden or 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
async function foo(args) { | |
console.log('before await'); | |
const value = await 'b'; | |
console.log('after await', value); | |
return value; | |
}; | |
//Simplified version of async foo function | |
function foo() { | |
let resolve; | |
let reject; | |
let value; | |
const promisse = new Promise((_resolve, _reject) => { | |
resolve = _resolve; | |
reject = _reject; | |
}); | |
function async_body(step) { | |
switch (step) { | |
case 0: { | |
console.log('before await'); | |
Promise.resolve('b').then(result => { | |
value = result; | |
async_body(step + 1); | |
}, | |
error => reject(error) | |
); | |
break; | |
} | |
case 1: { | |
console.log('after await', value); | |
resolve(value); | |
break; | |
} | |
} | |
default: | |
resolve(); | |
}; | |
try { | |
async_body(0); | |
} catch (e) { | |
reject(e); | |
} | |
return promisse; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment