Javascript 链接承诺实现提供已解决的错误

Javascript 链接承诺实现提供已解决的错误,javascript,Javascript,我一直在努力将这些承诺联系在一起,如下所示: doAsyncWork(123) .then(function (result){ return doAnotherAsync();}) .then(function (result2){ return displayText(result2)} function doAsyncWork(value){ return new Promise(function(resolve, reject)

我一直在努力将这些承诺联系在一起,如下所示:

 doAsyncWork(123)
   .then(function (result){
        return doAnotherAsync();})
   .then(function (result2){
         return displayText(result2)}

function doAsyncWork(value){
     return new Promise(function(resolve, reject){
                var time  = setTimeOut(function (){
                    resolve(value)}, Math.random * 1000);
                 });
      }

 function doAnotherAsyn(){
       return new Promise(function(resolve, reject){
                  var time = setTimeOut(function (){
                      resolve(456)}, Math.random * 100);
        });}
输出为:

123

未定义

未捕获错误:承诺已解决

在上述代码中创建的Promises对象实现为

Promise = (function()
{/**
 * Promise Status Enumerator
 * @private
 */
var Status = {
    Pending: 0,
    Resolved: 1,
    Rejected: 2
};

var Promise = function (fn) {
    /**
     * @private
     * @type {Status}
     */
    this.status = Status.Pending;
    /**
     * @private
     * @type {object}
     */
    this.result = undefined;
    /**
     * @private
     * @type {object}
     */
    this.reason = undefined;
    /**
     * @private
     * @type {Array}
     */
    this.queue = [];

    if (typeof fn !== "function") {
        reject.call(this, new Error('invalid executor'));
    }

    try {//catch errors
        fn.call(null, resolve.bind(this), reject.bind(this));
    } catch (e) {
        reject.call(this, e);
    }
};


Promise.prototype = {
    /**
     * Appends fulfillment and rejection handlers to the promise and return
     * a new promise object
     */
    then: function (onResolved, onRejected) {
        if (this.status === Status.Resolved) {
            return new Promise(function (resolve, reject) {
                resolve(isFunction(onResolved) ? onResolved.call(null, this.result) : undefined);
            }.bind(this));
        }

        if (this.status === Status.Rejected) {
            return new Promise(function (resolve, reject) {
                reject(isFunction(onRejected) ? onRejected.call(null, this.reason) : undefined);
            }.bind(this));
        }

        if (this.status === Status.Pending) {
            //pending
            var instance = this;
            return new Promise(function (resolve, reject) {
                //run resolve or reject object when parent's resolve or reject is called
                instance.queue.push({
                    resolve: function (result) {
                        resolve(isFunction(onResolved) ? onResolved.call(null, result) : undefined);
                    },
                    reject: function (reason) {
                        reject(isFunction(onRejected) ? onRejected.call(null, reason) : undefined);
                    }
                });
            });
        }
    },

    /**
     * Appends a rejection handler callback to the promise and returns a new promise resolving to the return value
     * of the callback
     *
     * @param {?function(error)} onRejected called if the promise is rejected
     */
    catch: function (onRejected) {
        return this.then(undefined, onRejected);
    }
};
/**
 * internal resolve function,simply assigns the result to the promise object
 * @private
 * @param {object} result
 */
function resolve(result) {

    var self = this;
    if (self.status !== Status.Pending) {
        throw "The Promise has already been resolved.";
    }

    if(result instanceof  Promise){

        self.result = result.then(function(innerResult){
            resolve(innerResult).bind(self);
            self.status = Status.Pending;
        });
    }
    else{
        this.result = result;
        this.status = Status.Resolved;
    }

    //call the queued listeners
    this.queue
        .map(function (item) {
            return item.resolve;
        })
        .forEach(function queueResolver(resolver) {
            resolver.call(null, result);
        });

    //delete queue
    this.queue = [];
}
我已经按照A+规范的承诺写了这篇文章。如果我没有提到任何规范,请告诉我。 请帮助我理解

1.创建的内部承诺状态(在doAnotherAsyn()中)在我的代码中是如何更改为解析的,此时它应该挂起的

2.在链接承诺概念中,我们如何区分外部承诺对象和内部承诺对象


3.我注意到上面代码中的“result2”是一个promise对象,它应该是函数中创建的内部promise的解析结果。如何解决这些问题?

您从哪里获得日志的输出?什么是
displayText
self.result=result.then(函数(innerResult){resolve(innerResult).bind(self);…
没有任何意义请先修复代码片段中的语法错误:
设置超时
,大括号等。感谢您的回复georg,我肯定会修复所有语法错误,然后返回self.result,如果函数返回一个承诺,我们应该在内部解决并正确返回结果?所以我是这样做的。您能指导我吗如何实现?不,您不能
返回
结果,您只能
解析。调用(self,innerResult)