async, await and promise not conflict, it may be an extension of promise
1, then just a split callback (previously callbak is also a function, but can split the back, of the assembly)
function WaitHandle () { var DTD .Deferred = $ (); // Create a deferred objects var the wait = function (DTD) { // requires a passing object is deferred var Task = function () { the console.log ( 'execution completion ' ); dtd.resolve (); // for asynchronous task has been completed // dtd.reject (); // indicating an asynchronous tasks fail or error } the setTimeout (task, 2000 ); return dtd.promise (); // where Back Promise, rather than directly go back deferreds } // Note that this value must be returned return the wait (DTD); } var w = waitHandle(); w.then(function(){ console.log('ok1') },function(){ console.log('error1') }).then(function(){ console.log('ok2') },function(){ console.log('error2'); })
2, aysnc / await it is the most direct synchronization wording (of the order of execution with the same wording)
import 'babel-polyfill'; function loadImg(src) { const promise = new Promise(function (resolve, reject) { var img = document.createElement('img'); img.onload = function(){ resolve(img); } img.onerror = function(){ reject(); } img.src = src; }) return promise; } const load = async function(){ const result1 = await loadImg('xxx.png'); console.log(result1); const result2 = await loadImg('yyy.png'); console.log(result2); } load();
usage
1, the await use, the function must be identified async
2, await followed example is a Promise
3, need babel-polyfill (meaning compatible)
npm i --save-dev babel-polyfill
to sum up
1, a Promise, and there is no conflict and Promise
2, the wording is completely synchronized, no callback function
3, however: js not change the nature of single-threaded, asynchronous