chenmnkken / easyjs

A simple javascript library

Home Page:http://easyjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

promise实例被放到when里面是,promise自己的then列表会被清除,导致无法执行下面的写法

acelan86 opened this issue · comments

            Promise.when(
                step1().then(function (data) {
                    alert('hahaha' + data);
                }),
                step2().then(function (data) {
                    alert('hahahah' + data);
                })
            ).then(function (data1, data2) {
                alert(data1 + ', ' + data2);
            });

只执行alert(data1 + ',' + data2); 因为这时候pmeCache中的step1,跟step2的promise都被delete了,随之resolveHandlers跟rejectHandlers也木有了~

谢谢你的反馈,最近比较忙,没看到,我会尽快排查。

@acelan86 你上面的Demo代码在使用上有点问题,easy.js 的 promise 设计的 E.when 方法只接受 promise 实例的参数,而你的 Demo 代码中传递给 when 方法的并不是 promise 实例。then 方法返回的并不是一个 promise 实例,then 方法返回的 this 是为了便于链式调用,这种错误的使用方法导致的 pmeCahce 中的列表被清除就不足为奇了。

正确的使用方法应该是这样的:

var showMsg1 = function(){
    var promise = new E.Promise();
    setTimeout(function(){
        alert( 'ok1' );
        promise.resolve( 'hello1' );
    }, 2000 );

    return promise;
};

var showMsg2 = function(){
    var promise = new E.Promise();
    setTimeout(function(){
        alert( 'ok2' );
        promise.resolve( 'hello2' );
    }, 8000 );

    return promise;
};


E.when( showMsg1(), showMsg2() ).then(function( str1, str2 ){
    alert( str1 + ' ' + str2 );
}).then(function(){
    alert( 'all promise is done' );
});