Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
// 例4
function getUserId() {
    return new Promise(function (resolve) {
        window.setTimeout(function () {
            resolve(9876);
        });
    });
}
function getUserMobileById(id) {
    return new Promise(function (resolve) {
      console.log('start to get user mobile by id:', id);
        window.setTimeout(function () {
            resolve(13810001000);
        });
    });
}
getUserId()
    .then(getUserMobileById)
    .then(function (mobile) {
        console.log('do sth with', mobile);
    });
function Promise(fn) {
    var state = 'pending',
        value = null,
        deferreds = [];
        
    this.then = function (onFulfilled) {
        return new Promise(function (resolve) {
            handle({
                onFulfilled: onFulfilled || null,
                resolve: resolve
            });
        });
    };
    function handle(deferred) {
        if (state === 'pending') {
            deferreds.push(deferred);
            return;
        }
    
        var ret = deferred.onFulfilled(value);
        deferred.resolve(ret);
    }
    
    function resolve(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);
    }
    fn(resolve);
}
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers