Tag: 承诺

NodeJS超时如果未能及时完成Promise

如何在一定的时间后超时? 我知道Q有一个承诺超时,但我使用本机NodeJS承诺,他们没有.timeoutfunction。 我错过了一个还是它的包装不同? 或者,下面的执行是否在不吸取内存,实际上按预期工作? 我也可以使它以某种方式包裹在全球范围内,所以我可以使用它创build的每一个承诺,而不必重复setTimeout和clearTimeout代码? function run() { logger.info('DoNothingController working on process id {0}…'.format(process.pid)); myPromise(4000) .then(function() { logger.info('Successful!'); }) .catch(function(error) { logger.error('Failed! ' + error); }); } function myPromise(ms) { return new Promise(function(resolve, reject) { var hasValueReturned; var promiseTimeout = setTimeout(function() { if (!hasValueReturned) { reject('Promise timed out after ' + ms + ' ms'); […]

在Node.js中提供一个Promise作为一个模块的导出asynchronous初始化的有效模式?

我需要编写一些加载数据的模块,然后为这些数据提供一个接口。 我想asynchronous加载数据。 我的应用程序已经使用承诺。 提供一个承诺,因为要求一个模块有效的模式/成语? 示例模块: var DB = require('promise-based-db-module'); module.exports = DB.fetch('foo') .then(function(foo){ return { getId: function(){return foo.id;}, getName: function(){return foo.name;} }; }); 用法示例: require('./myPromiseModule') .then(function(dataInterface){ // Use the data }); 更新: 我已经使用了一段时间了,而且效果很好。 我已经学到了一件事,而且在接受的答案中暗示的一点是,caching诺言本身,以及随时随地访问数据使用都是好事。 数据第一次被访问时,代码将等待,直到承诺解决。 接下来的使用将立即返回数据。 例如 var cachedPromise = require('./myPromiseModule'); cachedPromise.then(function(dataInterface){ // Use the data }); … cachedPromise.then(function(dataInterface){ // Use the data again somewhere […]

我如何提交AWS JavaScript SDK?

我想用promise来使用JavaScript中的aws-sdk。 而不是默认的callback风格: dynamodb.getItem(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); 我反而想用一个承诺风格: dynamoDb.putItemAsync(params).then(function(data) { console.log(data); // successful response }).catch(function(error) { console.log(err, err.stack); // an error occurred });

如果我在Kriskowal的q中多次拒绝/解决,会发生什么?

我正在研究promise模式,并使用kriskowal的q for node.js, 有这个片段: var deferred = Q.defer(); try { messageData = JSON.parse(message); } catch (e) { global.logger.warn('Error parsing JSON message.'); deferred.reject(e); } … if (some_reason) deferred.resolve(something); … return deferred.promise; 如果parsing器都失败了, some_reason是真的呢? 执行是否会从拒绝到解决,双方都会在不同的时间被调用,从而产生一个bug? 我应该避免多次拒绝/解决?

在node.js中嵌套承诺是正常的吗?

在学习node.js的过程中,我一直在努力学习两个星期的问题是如何使用节点进行同步编程。 我发现,无论我如何尝试按顺序做事,总是会得到嵌套的承诺。 我发现有一些模块,比如Q,可以帮助保证链接的可维护性。 在做研究的时候,我不明白的是Promise.all()和Promise.resolve()和Promise.reject()。 Promise.reject与名称相差无几,但在编写应用程序时,我很困惑如何将这些函数或对象中的任何一个包含进来,而不会破坏应用程序的行为。 当来自Java或C#等编程语言时,node.js肯定有学习曲线。 仍然存在的问题是在node.js中承诺链接是否正常(最佳实践)。 例 driver.get('https://website.com/login').then(function () { loginPage.login('company.admin', 'password').then(function () { var employeePage = new EmployeePage(driver.getDriver()); employeePage.clickAddEmployee().then(function() { setTimeout(function() { var addEmployeeForm = new AddEmployeeForm(driver.getDriver()); addEmployeeForm.insertUserName(employee.username).then(function() { addEmployeeForm.insertFirstName(employee.firstName).then(function() { addEmployeeForm.insertLastName(employee.lastName).then(function() { addEmployeeForm.clickCreateEmployee().then(function () { employeePage.searchEmployee(employee); }); }); }); }); }, 750); }); }); });

什么是最好的方式将解决的承诺价值传递给最终的“当时”链

我试图让我的头在承诺,在node.js使用Q模块,但我有一个小问题。 在这个例子中: ModelA.create(/* params */) .then(function(modelA){ return ModelB.create(/* params */); }) .then(function(modelB){ return ModelC.create(/* params */); }) .then(function(modelC){ // need to do stuff with modelA, modelB and modelC }) .fail(/*do failure stuff*/); .create方法在每个.then()中返回一个promise,如预期的那样获得promise的parsing值。 然而在最后.then()我需要有所有3先前解决的承诺值。 什么是最好的方法来做到这一点?

连锁的承诺没有通过拒绝

我有一个问题,理解为什么拒绝不通过承诺链传递,我希望有人能够帮助我理解为什么。 对我来说,将function附加到一系列的承诺意味着我的意图是我依靠一个原始的承诺来实现。 这很难解释,所以让我首先展示一个我的问题的代码示例。 (注意:这个例子是使用Node和延迟节点模块,我用Dojo 1.8.3testing过,结果相同) var d = require("deferred"); var d1 = d(); var promise1 = d1.promise.then( function(wins) { console.log('promise1 resolved'); return wins;}, function(err) { console.log('promise1 rejected'); return err;}); var promise2 = promise1.then( function(wins) { console.log('promise2 resolved'); return wins;}, function(err) { console.log('promise2 rejected'); return err;}); var promise3 = promise2.then( function(wins) { console.log('promise3 resolved'); return wins;}, […]

那么如何处理if-else呢?

在某些情况下,当我从一个promise对象中得到一个返回值时,我需要根据这个值的条件来启动两个不同的then() ,如: promise().then(function(value){ if(//true) { // do something } else { // do something } }) 我想也许我可以这样写: promise().then(function(value){ if(//true) { // call a new function which will return a new promise object ifTruePromise().then(); } else { ifFalsePromise().then(); } }) 但是有了这个,我有两个问题: 我不确定是否有一个好的主意来开始一个新的承诺 – 然后是一个承诺的过程; 如果我需要这两个过程在最后调用一个函数呢? 这意味着他们有相同的“terminal” 我试图返回新的承诺,以保持原来的链像: promise().then(function(value){ if(//true) { // call a new function which […]

使用承诺 – 在失败处理程序中logging堆栈跟踪

我对nodejs比较陌生,所以我会更详细地解释一下我正在做的事情。 我有一个networking服务器。 如果请求失败,我想logging该exception的堆栈跟踪,但是传递错误页面而不会使服务器崩溃。 例如,处理请求的函数: var Q = require('q'); var requestHandler = function () { // Here I get the data etc. that was requested. As this is not important, just a dummy here Q.resolve() // Now I answer the request .then(function (data) { // Dummy Code representing the sending of a response console.log('sending response …'); […]

蓝鸟Promise.all – 多个承诺完成聚合成功和拒绝

今天有人用蓝鸟提出一个有趣的案例,处理多重承诺的最佳方式是什么,我们不想停止某个履行或拒绝,而是对检查最终结果感兴趣。 一个例子: var p1 = new Promise(function(f,r){ setTimeout(function(){ console.log("p1"); f("yay"); }, 100); }); var p2 = new Promise(function(f,r){ setTimeout(function(){ console.log("p2"); r(new Error("boo")); }, 200); }) var p3 = new Promise(function(f,r){ setTimeout(function(){ console.log("p3"); r(new Error("yay")); }, 300); }); var p4 = new Promise(function(f,r){ setTimeout(function(){ console.log("p4"); f("yay"); }, 400); }); //Promise.all([p1,p2, p3, p4]).then(function(p){ // console.log("Results:",p); //}).error(function(e){ // […]