httpinvoke和pouchdb承诺停滞

时间:2015-01-07 19:35:07

标签: couchdb promise

在进行httpinvoke调用之后,我需要加载couchDB,但承诺没有传递。

createDB: function() {
    var db = new PouchDB('options');
    db.info().then(function (info){
        if(info.doc_count <= 10000) {
            var db = new PouchDB('options');
            db.destroy().then(function(info){
                httpinvoke('http://localhost:9080/secure/sync.form','GET');
            }).then(function (res){
                console.log(JSON.stringify(res)); //This never gets called but if I move this then() block to httpinvoke(xxx).then()  it does get called
            }).catch(function(err){
                console.log(JSON.stringify(err));
            });
        }
    });
}

1 个答案:

答案 0 :(得分:0)

承诺链返回值。如果你想通过承诺做出任何有意义的事情,你必须返回它。 promise表示值+时间,您的httpinvoke调用未返回类似于不返回的同步函数。

createDB: function() {
    var db = new PouchDB('options');
    db.info().then(function (info){
        if(info.doc_count <= 10000) {
            var db = new PouchDB('options');
            db.destroy().then(function(info){
                return httpinvoke('...','GET'); // NOTE THE RETURN
            }).then(function (res){
                console.log(res); // console already shows JSON 
            }); // no need to `catch anyway`, errors are errors let's not suppress. 
        }
    });
}

还要注意承诺