, , , , . , Promises ECMAscript 7 ( JavaScript), :
function sleep(milliseconds) {
return new Promise(function(resolve, _) {
setTimeout(resolve, milliseconds);
});
}
async function helloAfter(seconds) {
console.log("Sleeping " + seconds + " seconds.");
await sleep(seconds * 1000);
console.log("Hello, world!");
}
helloAfter(1);
console.log("Script finished executing.");
:
Sleeping 1 seconds.
Script finished executing.
Hello, world!
( Babel)
, , , , sleep . , , Promise, .
helloAfter async, . , , , helloAfter Promise . "Script ". ", !".
helloAfter async await . . await sleep(seconds * 1000); , helloAfter , , sleep, . , : - helloAfter. , helloAfter , "Hello, world!". .
async/await, ES7.