//例1 getUserId() .then(getUserMobileById) .then(function (mobile) { // do sth else with mobile }, function (error) { // getUserId或者getUerMobileById时出现的错误 console.log(error); });
functionhandle(deferred) { if (state === 'pending') { deferreds.push(deferred); return; }
var ret = deferred.onFulfilled(value); deferred.resolve(ret); }
functionresolve(newValue) { if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { var then = newValue.then; if (typeof then === 'function') { then.call(newValue, resolve); return; } } state = 'fulfilled'; value = newValue; setTimeout(function () { deferreds.forEach(function (deferred) { handle(deferred); }); }, 0); }
回到例1
1 2 3 4 5 6 7 8
getUserId() .then(getUserMobileById) .then(function (mobile) { // do sth else with mobile }, function (error) { // getUserId或者getUerMobileById时出现的错误 console.log(error); });