Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Last active June 3, 2017 09:51
Show Gist options
  • Save gskachkov/d1322c29fe2f6ff3c3ba670f2a6f4ff2 to your computer and use it in GitHub Desktop.
Save gskachkov/d1322c29fe2f6ff3c3ba670f2a6f4ff2 to your computer and use it in GitHub Desktop.
Async function as plain function
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