Skip to content

Instantly share code, notes, and snippets.

@wdjunaidi
Created September 24, 2017 18:52
Show Gist options
  • Save wdjunaidi/2f2e17644d94b540bd708e30e6a36fd0 to your computer and use it in GitHub Desktop.
Save wdjunaidi/2f2e17644d94b540bd708e30e6a36fd0 to your computer and use it in GitHub Desktop.
Playing with JavaScript async await behaviour
async function one() {
return 1;
}
const onePromise = one();
console.log(`one: ${onePromise}`);
onePromise.then(data => console.log(`one data: ${data}`));
async function nothing() {
console.log('in nothing');
}
const nothingResult = nothing();
console.log(`nothing: ${nothingResult}`);
nothingResult.then(data => console.log(`nothing data: ${data}`));
const Test = {
first: async () => {
console.log('in first()');
return "uno";
},
second: async () => {
console.log('in second()');
return "dos";
},
third: async () => {
console.log('in third()');
return "tres";
}
}
async function test() {
console.log('test a');
const a = Test.first();
console.log(`test a: ${a}`);
console.log('test b');
const b = Test.second();
console.log(`test b: ${b}`);
console.log('test c');
const c = Test.third();
console.log(`test c: ${c}`);
}
test();
async function testAwait() {
console.log('testAwait a');
const a = await Test.first();
console.log(`testAwait a: ${a}`);
console.log('testAwait b');
const b = await Test.second();
console.log(`testAwait b: ${b}`);
console.log('testAwait c');
const c = await Test.third();
console.log(`testAwait c: ${c}`);
}
testAwait();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment