简体   繁体   English

Javascript Kriskowal Q JS承诺不起作用

[英]Javascript Kriskowal Q JS promise not working

I have created a promise using kriskowal/q module but when i try to use this it does not go into any function either happy path or error path. 我已经使用kriskowal / q模块创建了一个promise,但是当我尝试使用它时,它不会进入任何函数,无论是快乐路径还是错误路径。

here is my promise creation class 这是我的诺言创作课

var Q = require('q');

var Test = function () {

};

Test.prototype = (function () {
    var testt = function () {
        var deferred = Q.defer();
        var x = 5;
        if (x === 5){
            deferred.resolve('resolved');
        }else{
            deferred.error(new Error('error'));
        }
        return deferred.promise;
    };
    return {
        testt : testt
    }
}());

module.exports = Test;

and this is how i am going to use it 这就是我要使用它的方式

var Test = require('../src/js/test.js');

describe("Test", function () {
    "use strict";

    var test = null;

    beforeEach(function () {
        test = new Test();
    });

    it("should return the promise", function () {
        test.testt().then(
            function (a) {
                console.log(a);
            },
            function (b) {
                console.error(b);
            }
        );
    });
});

since this is a jasmine test class if your not familiar with jasmine, what is inside 'it' function is the logic how i am using the promise. 由于这是一个茉莉花测试类(如果您不熟悉茉莉花),因此“ it”函数内部的逻辑是我使用诺言的逻辑。 And the 'testt' is the function where i create the promise. 而“ testt”是我创建承诺的功能。 for more clarification i have attached the entire code. 为了更清楚,我附上了整个代码。

Issue : It does not print either a or b 问题:它不打印a或b

Your it is finishing immediately, instead of after the promise's resolution/rejection. it立即完成,而不是在承诺的解决/拒绝之后。

it("should return the promise", function (done) {
    test.testt().then(
        function (a) {
            console.log(a);
            done();
        },
        function (b) {
            console.error(b);
            done();
        }
    );
});

See here for more info. 有关更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM