httpinvoke 和 pouchdb 承诺停滞

httpinvoke and pouchdb promise stalling

调用 httpinvoke 后,我需要加载 couchDB,但 promise 没有传递。

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));
            });
        }
    });
}

Promises 链由 return 值组成。如果你想通过承诺使任何事情变得有意义,你必须 return 它。承诺代表价值 + 时间,您的 httpinvoke 调用不 return 类似于不 returning 的同步函数。

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. 
        }
    });
}

另请注意承诺