promise

let foo = () => new Promise((resolve, reject) => {

  let canResolve = true;

  if (canResolve) {

    resolve('Resolved');

  } else {

    reject('failed');

  }

})

foo().then(data => console.log(data), reason => console.log(reason));

resolve将在promise主体执行完后调用then的第一个callback,reject调用第二个.

你可能感兴趣的:(promise)