Promise how to implement in this situation?

i have a problem with sync using fs.readfile, they are not inicializing at the first time, if i try request again, then i have results. I know i can use promise and i find something useful with Q from Kriskowal. I tried it but without success. I didn´t understand very well how to implement it. If someone can help with this, i will be eternally grateful. code: "use strict" var Q = requi

承诺如何在这种情况下实施?

我有一个使用fs.readfile同步的问题,他们在第一次没有inicializing,如果我再次尝试请求,然后我有结果。 我知道我可以使用承诺,并且我从Kriskowal找到一些有用的Q. 我尝试过但没有成功。 我不太清楚如何实施它。 如果有人能够帮助,我将永远感激。 码: "use strict" var Q = require('q'); var fs = require('fs'); var arrayZipBand = []; var jsonZipCode = []; var arrayParsedZipcodeBr = []; exports.p

Javascript Promises nest deeply just like callbacks

I am trying to use Promises with my async functions. From my understanding promises are supposed to alleviate callback hell. However it seems my code is still very deeply nested. Am I missing something about promises (could this be rewritten to be more readable/idiomatic) or do promises nest just like callbacks sometimes? In this example I'm building a function that calls an API to retr

Javascript Promises嵌套深度就像回调

我正在尝试使用Promises和异步函数。 根据我的理解,诺言应该可以缓解回拨地狱。 不过看起来我的代码还是非常深的嵌套。 我是否错过了一些有关承诺的内容(可以将其重写为更具可读性/惯用性),或者有时会像回调一样做承诺嵌套? 在这个例子中,我构建了一个调用API从一些社交媒体帐户中检索朋友列表的函数。 如果由于认证错误导致初始呼叫失败,我知道我必须先认证并再次进行呼叫。 const myRequest = { /* custom insta

Promise waterfall

This question already has an answer here: How to structure nested Promises 2 answers You've got a promise anti pattern happening. You can return promises from promises to avoid nesting the promises like you've done. promiseOne() .then(() => promiseTwo()) .then(() => promiseThree()) .then(() => promiseFour()); By the way Node supports the built in Promise constr

诺言瀑布

这个问题在这里已经有了答案: 如何构建嵌套Promise 2答案 你有承诺反模式发生。 您可以从承诺中返还承诺,以避免像您所做的那样嵌套承诺。 promiseOne() .then(() => promiseTwo()) .then(() => promiseThree()) .then(() => promiseFour()); 顺便说一下,Node支持内置的Promise构造函数。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise const pro

How do I access previous promise results in a .then() chain?

I have restructured my code to promises, and built a wonderful long flat promise chain , consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results . However the resolution values from the middle of the sequence are not in scope in the last callback, how do I access them? function getExample() { retur

如何在.then()链中访问先前的承诺结果?

我已经将我的代码重组为承诺,并构建了一个美妙的长平诺言链 ,由多个.then()回调组成。 最后,我想返回一些复合值,并需要访问多个中间承诺结果 。 但是,从序列中间的分辨率值不在最后一次回调的范围内,我如何访问它们? function getExample() { return promiseA(…).then(function(resultA) { // Some processing return promiseB(…); }).then(function(resultB) { // More processing

What is the explicit promise construction antipattern and how do I avoid it?

I was writing code that does something that looks like: Using the Promise constructor: function getStuffDone(param) { return new Promise(function(resolve, reject) { myPromiseFn(param+1) .then(function(val) { resolve(val); }) // or .then(resolve) .catch(function(err) { reject(err); }); // or .catch(reject) }); } Using Q or deferred: function getStuffDo

什么是显式承诺构造反模式,我该如何避免它?

我正在编写的代码看起来像这样: 使用Promise构造函数: function getStuffDone(param) { return new Promise(function(resolve, reject) { myPromiseFn(param+1) .then(function(val) { resolve(val); }) // or .then(resolve) .catch(function(err) { reject(err); }); // or .catch(reject) }); } 使用Q或延期: function getStuffDone(param) { var d = Q.defe

Js Deferred/Promise/Future compared to functional languages like Scala

I'm mostly using programming languages like Scala and JavaScript. I'm trying to understand the similarities and differences in how async reactive programming is used in both languages. Can you help me? I'm not taking any particular Js Promise framework because it seems many implement the similar specifications (like Promise/A). I've only used Q so far. It seems that in Java

Js延期/承诺/未来与Scala等功能语言相比

我主要使用Scala和JavaScript等编程语言。 我试图理解在两种语言中如何使用异步响应编程的相似之处和不同之处。 你可以帮我吗? 我没有采用任何特定的Js Promise框架,因为它似乎很多实现了类似的规范(如Promise / A)。 到目前为止,我只用过Q. 似乎在JavaScript中,我们称之为Deferred了我们决心完成Promise的对象。 在Scala中, Promise似乎是你决心获得Future monad的对象。 有人可以告诉我这是对的吗? 在Js和S

Making Async AJAX calls Synchronous

Environment: Ext-JS Frontend UI -> AJAX Calls -> Spring MVC Server/Java -> Apache CXF to MQ -> Back-end COBOL. Based on the above environment we can not handle asynchronous AJAX calls being sent due to a limitation with the backend COBOL server. Normally we daisy chain our ajax calls. However now we need an automatic refresh mechanism on some UI pages. So if we happen to send a n

使异步AJAX调用同步

环境:Ext-JS前端UI - > AJAX调用 - > Spring MVC Server / Java - > Apache CXF到MQ - >后端COBOL。 基于上述环境,由于后端COBOL服务器的限制,我们无法处理发送的异步AJAX调用。 通常我们菊花链我们的ajax调用。 但是现在我们需要在某些UI页面上使用自动刷新机制。 因此,如果我们在发送刷新(AJAX)调用的同时发送正常的UI请求,那么我们需要对这两个调用进行排队并使它们有效地同步。 任何解决方案都

How to make an AJAX call without jQuery?

如何使用JavaScript进行AJAX调用,而不使用jQuery? With "vanilla" JavaScript: <script type="text/javascript"> function loadXMLDoc() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4 if (xmlhttp.status == 200) { document.getElementBy

如何在没有jQuery的情况下进行AJAX调用?

如何使用JavaScript进行AJAX调用,而不使用jQuery? 用“香草”JavaScript: <script type="text/javascript"> function loadXMLDoc() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4 if (xmlhttp.status == 200) { document.getElementById("myDiv").inne

jQuery: Return data after ajax call success

This question already has an answer here: How do I return the response from an asynchronous call? 33 answers The only way to return the data from the function would be to make a synchronous call instead of an asynchronous call, but that would freeze up the browser while it's waiting for the response. You can pass in a callback function that handles the result: function testAjax(handle

jQuery:在Ajax调用成功后返回数据

这个问题在这里已经有了答案: 如何返回来自异步调用的响应? 33个答案 从函数中返回数据的唯一方法是进行同步调用而不是异步调用,但这会在浏览器等待响应时冻结浏览器。 你可以传递一个处理结果的回调函数: function testAjax(handleData) { $.ajax({ url:"getvalue.php", success:function(data) { handleData(data); } }); } 像这样调用它: testAjax(function(output){ // here you u

Is there any reason to use a synchronous XMLHttpRequest?

It seems most everyone does asynchronous requests with XMLHttpRequest but obviously the fact that there is the ability to do synchronous requests indicates there might be a valid reason to do so. So what might that valid reason be? I think they might become more popular as HTML 5 standards progress. If a web application is given access to web workers, I could foresee developers using a dedica

有什么理由使用同步XMLHttpRequest?

看起来大多数人都使用XMLHttpRequest进行异步请求,但显然有能力执行同步请求的事实表明可能有这样做的正当理由。 那么这个合理的原因是什么? 我认为随着HTML 5标准的进展,它们可能会变得更受欢迎。 如果一个Web应用程序可以访问Web工作人员,我可以预见开发人员使用一个专门的Web工作人员来提出同步请求,就像Jonathan说的那样,以确保在另一个请求之前发生一个请求。 对于一个线程的当前情况,这是一个不太理想的设计,