Skip to content

Instantly share code, notes, and snippets.

@derzunov
Last active August 18, 2016 12:45
Show Gist options
  • Save derzunov/451469c21c53c7e7d282f638598383df to your computer and use it in GitHub Desktop.
Save derzunov/451469c21c53c7e7d282f638598383df to your computer and use it in GitHub Desktop.
function asyncFunc1 () {
console.log("Выполняюсь 1");
return new Promise(function(resolve, reject) {
setTimeout( () => resolve({message: "First done!"}), 200 );
});
};
function asyncFunc2 () {
console.log("Выполняюсь 2");
return new Promise(function(resolve, reject) {
setTimeout( () => resolve({message: "Second done!"}), 400 );
});
};
function asyncFunc3 () {
console.log("Выполняюсь 3");
return new Promise(function(resolve, reject) {
setTimeout( () => resolve({message: "Third done!"}), 600 );
});
};
function callback ( data ) { console.log( data.message ) };
async function startAsync () {
// Работают синхронно, в нужном нам порядке
setTimeout(() => console.log("Some Task"), 500);
let data1 = await asyncFunc1();
let data2 = await asyncFunc2();
let data3 = await asyncFunc3();
console.log("startAsync:", data1.message, data2.message, data3.message);
};
startAsync();
/*
В консоли получим:
Выполняюсь 1
Выполняюсь 2
Some Task
Выполняюсь 3
startAsync: First done! Second done! Third done!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment