Tag: 承诺

量angular器:testing总是超时,不显示诺言输出

browser.sleep(4000); var discount_type = element(by.id("discount_type")); var discount_value= element(by.id("discount_value")); var grandTotal = element(by.id("grand-total")); var subtotal = element(by.id("subtotal")); var delivery_fee = element(by.id("delivery_fee")); var last = protractor.promise.defer(); console.log("hallo"); subtotal.getText().then(function(sub) { console.log("#############"); console.log(sub); delivery_fee.getText().then(function(fee) { console.log(fee); var calc = parseFloat(sub) – parseFloat(fee) – parseFloat(config.promocodes.referral.discount); console.log(calc); last.fulfill(calc); }); }); last.then(function(calc) { console.log("final wait"); expect(grandTotal.getText()).toBe("$" + calc); }); 在确定我的testing运行正常之前,我需要计算一个值。 在我的testing中,我总是在控制台中看到“hallo”。 然后我得到一个 […]

通过Node.js传递从承诺到承诺的价值

我试图传递一个对象,我使用Firebase承诺一个一个地构build。 如果有更好的方法逐步构build对象,我并不需要沿承诺链传递对象。 这是我的代码: var queue = new Queue(productUpdateQueue, function(data, progress, resolve, reject) { var incomingUpdateData = data; var receiptID = incomingUpdateData.receiptID; var userID = incomingUpdateData.userID; var oldProductID = incomingUpdateData.oldProductID; var newProductID = incomingUpdateData.newProductID; var newReceipt = incomingUpdateData.newReceipt; var postID = ""; var updateObject = {}; updateObject['usersPrivate/'+userID+'/receipts/'+receiptID+'/items/'+oldProductID] = null; updateObject['usersPrivate/'+userID+'/receipts/'+receiptID+'/items/'+newProductID] = newReceipt; clicks.child('VigLink').orderByChild('item').equalTo(oldProductID).once('value', function(cuidSnapshot) { return […]

响应在结果可以从承诺中收集之前终止

在下面的代码片段中, lineReader监听一个事件line 。 当收到一个line事件时,它会在Parser上调用da ,并返回一个Promise lineReader.on('line',(line) => { Parser.da(line).then((info) => { }); }); lineReader.on('close',() => { req.end(); }); Parser.da = function(line) { return new Promise((resolve,reject) => { geo.getLocation().then((r) => { console.log(r); return resolve(r); }); }); } da函数调用一个也在Promise运行的函数。 会发生什么是我永远不会看到来自geo.getLocation和readLine.on('close')被调用的输出。 应该怎样处理这种情况呢?

使用承诺来控制stream量工作不正常

我试图控制下面代码中的执行stream程,这意味着我希望它是串行的。 我正在读取和更新数据到我的数据库,并且我希望以正确的顺序发生。 下面是我打电话给我的数据库的function,查询function被封装在callback中。 我对承诺相当陌生,所以也许错误可能是我忽略的一些愚蠢的东西。 如果你需要任何问题,请这样做。 function my_function(array, array2) { var array3 = []; return Promise.resolve(true) .then(function() { console.log("1") for(var i=0; i< array.length; i++) { get(array[i], function(results){ console.log("2") array3.push(..); }); } return array3; }).then(function() { console.log("3") for(var i=0; i< array2.length; i+=2) { //… get(array2[i], function(results){ console.log("4") return array3.push(…); }); } return array3; }).then(function(array3) { console.log("5") for(var […]

Node.js vs Browser:链接被拒绝的承诺

如果我在Chrome版本56.0.2924.87(64位)上试用此项 – 预期结果.. Promise.reject(null).then(console.log); > Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: null} 而如果我尝试节点v7.8.0 – 可能的错误? Promise.reject(null).then(console.log); > Promise { <pending> } 我做错了什么或者这是一个错误? (我假设前者) 我有麻烦通过处理链嘲笑拒绝的承诺,因为拒绝不通过链:( 查看MDN文档,我想我已经得到了正确的语法: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

如何在Restify中间件中捕获未处理的Promise拒绝?

我在Node.js上写了一个Restify应用程序 而我需要有一个函数,当Restifycallback中发生一些错误时会被调用。 这个函数应该发送一个响应给客户端。 如果我不使用承诺或asynchronous函数,我可以做这样的事情: server.on('uncaughtException', (req, res, route, err) => { log.error(err); res.send(err); }); 但是,如果我的callback是async函数,那么函数内的exception不会被调用,而是引发unhandledPromiseRejection 。 我如何重构我的代码,以便处理未处理的Promise拒绝,就像处理常见错误一样? 是的,我知道我可以检查Restify中间件本身的错误,但是它仍然会使代码更复杂,写起来更容易 if (!core.validateUUID(req.params.deviceId)) { throw new Error(`Device ${req.params.deviceId} is wrong`) } 然后 if (!core.validateUUID(req.params.deviceId)) { return next(new restify.InvalidArgumentError({ body: { success: false, error: `Device ${req.params.deviceId} is wrong`, } })); }

在节点中testing失败的请求

我有一些如下所示的代码: var request = require('request'); function Service(){ this._config = require('../path/to/config.json'); } Service.prototype.doThing = function(){ return new Promise(function(resolve, reject){ request.post(url, {payload}, function(error, response, body){ //handle response resolve(true); }).on('error', function(err){ //handle errors resolve(false); }); }); } 我试图testing运行错误的块,但由于承诺而有困难。 我正在使用摩卡进行testing运行,并使用了sinon进行testing。 我能够存根请求,以便我可以计算on方法被调用的次数,但是包含的Promise永远不会parsing。 有一些软件包可以和sinon一起处理promise(我已经尝试过sinon-promise和sinon-stub-promise),但是我必须存储整个doThing方法才能正确parsing。 我将不胜感激在任何方式来testing这个代码或替代的代码结构,可能更容易testing的input。 有问题的testing(等待doThing承诺返回)如下: context('when the server is unavailable', function(){ beforeEach(function() { var onStub = sinon.stub(); requestStub = {post: […]

node.js pg-promise和来自API的分页

看看https://github.com/vitaly-t/pg-promise/wiki/Data-Imports有一个关于如何使用它进行导入的非常详细的文档。 但是,虽然这适用于演示场景,但我不知道如何将其应用于我的案例。 当我进行networking调用时,我得到了实际的JSON数据和标题中的一个参数,它给了我下一页的值(可以是date或string或数字值)。 在这个例子中,它说: db.tx('massive-insert', t => { return t.sequence(index => { return getNextData(index) .then(data => { if (data) { const insert = pgp.helpers.insert(data, cs); return t.none(insert); } }); }); }) .then(data => { console.log('Total batches:', data.total, ', Duration:', data.duration); }) .catch(error => { console.log(error); }); 在这种情况下, sequence(index将使用似乎增加+1的索引,但在我的情况下, function getNextData(nextPage) { //get the data for […]

我怎样才能从Google地方获得一系列地点的坐标,然后在结束之后使用结果?

我有一些地名,例如: const places = ['King Square, London', 'Empire State Building', 'Great Wall of China']; 我需要一个新的对象数组,其中包含为地名数组中的每个元素设置的位置,例如: const places = [ { name: 'King Square, London', position: { latitude: …, longitude: … } }, … ]; 我想我必须做这样的事情: const places = []; ['King Square, London', 'Empire State Building', 'Great Wall of China'].forEach(placeName => { axios .get(`https://maps.googleapis.com/maps/api/place/textsearch/json?key=GOOGLE_PLACES_API_KEY&query=${placeName}`) .then(response => […]

RxJS Observable from CLI和应用程序中的交互循环

我正在使用node.js构build一个简单的命令行工具。 在过去,我正在使用这样的Promise方法 function listenCommand(){ inquirer.prompt([{ type:'input', name:'value', message:"Enter commande :" }]).then(function (command) { processCmd(command); }); } function processCmd(){ … listenCommand() } 这将创build我的主循环input命令。 当命令执行时,应用程序将要求下一个。 我现在正在尝试将其转换为类似的RxJS方法。 function listenCommand(){ let listener = Rx.Observable.fromPromise(inquirer.prompt([{ type:'input', name:'value', message:"Enter commande :" }])); listener.subscribe(function (command) { processCmd(command); }); } function processCmd(){ … listenCommand() } 它的工作原理,但这听起来不太好。 用RxJS做这个快速循环的正确方法是什么? 还是RxJS不适合这种工作,我应该坚持承诺的方法呢?