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>
 
// 例3
function getUserId() {
    return new Promise(function (resolve) {
        resolve(123);
    });
}
var promise = getUserId();
window.setTimeout(function () {
    promise.then(function (id) {
        console.log('do sth with', id);
    });
}, 1000);
function Promise(fn) {
    var state = 'pending',
        value = null,
        deferreds = [];
        
    this.then = function (onFulfilled) {
        if (state === 'pending') {
            deferreds.push(onFulfilled);
            return this;
        }
        onFulfilled(value);
        return this;
    };
    
    function resolve(newValue) {
        value = newValue;
        state = 'fulfilled';
        setTimeout(function () {
            deferreds.forEach(function (deferred) {
                deferred(value);
            });
        }, 0);
    }
    fn(resolve);
}
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers