我读了一篇关于javascript承诺()的文档,其中一个例子使用了一系列承诺。
// Start off with a promise that always resolves
var sequence = Promise.resolve();
// Loop through our chapter urls
story.chapterUrls.forEach(function(chapterUrl) {
// Add these actions to the end of the sequence
sequence = sequence.then(function() {
以下javascript函数来自。根据我的理解,// start doing something that could take a while的第一个评论是有误导性的。代码中实际上可以异步完成的部分在传递给构造函数的函数中。
function foo(x) {
// start doing something that could take a while *misleading comment*
// construct and return a promise
return new Promise( /* executor */ function(resolve
我是javascript的新手,试图理解异步函数调用。下面是我的js程序:
var notify = function () {
console.log('before');
doPost('data', (res) => {
console.log('callback received');
});
console.log('after');
}
var doPost = function (data) {
console.log(data);
while
描述说它返回一个布尔值,但javascript文档声明它返回一个promise: /**
* Returns true if there are any elements present that match the finder.
*
* @alias element.all(locator).isPresent()
*
* @example
* expect($('.item').isPresent()).toBeTruthy();
*
* @returns {Promise<boolean>}
*/
我正在尝试对不同风格的javascript代码进行一些基准测试,以下是我所拥有的:
var Promise = require('bluebird');
var timer = function(name) {
var start = new Date();
return {
stop: function() {
var end = new Date();
var time = end.getTime() - start.getTime();
console.log(
最近,我看到在AngularJS和JQuery中实现了Promise的概念。
我在下面的代码中看到了Java中Futures的实现,但是这需要在语言/平台中提供线程池的概念。然而,Javascript中没有这样的线程概念。那么Javascript中的Promise是如何实现的呢?
public class Futures1 {
private static final ExecutorService pool = Executors
.newFixedThreadPool(10);
public static void main(String[] a
是否有规范/实现细节,说明对承诺回调的评估有多“急切”?我是说,假设我有
var promise = new Promise((resolve) => {
resolve()
});
console.log(promise); // there is no public field '<state>', but you can see it in the console of the dev tools
我看到promise已经实现了;我原以为Promise内部实现会在以后的某个时间调用resolve回调,留下一个“时间窗口”,而promise
将函数设为异步会使其异步吗?
我开始使用async-await代替promise chain。我做了一些像这样的事情
async function f(){
let r = await first();
let d = await sec(r);
return d;
}
在调用此函数时,我能够看到所有代码都是异步执行的。但是在我读到的一些老文章中,我们不能在JavaScript中创建异步函数。那么,使函数异步是否会使其异步呢?
请容忍我作为一名Java程序员,在一个新项目中学习现代JavaScript。我理解了Promises处理异步操作的概念,但是有没有理由“简化”代码,即强度方面几乎什么也不做,也不包含任何你需要等待的东西(如db查询、http请求等)?我遇到了这个后端node.js代码,它在promise中做一些微不足道的事情:
const customersWithHeader; //assume this contains an array of rows loaded from CSV file
const allowedHeaderNames; //assume a string array with
火焰警报
它是如此简单:我非常熟悉Java可完成的未来:
CompleteableFuture f = CompleteableFuture.supplyAsync(() -> "Hello").thenApply(s -> s.concat(" world"))
遵循这一守则的可能是:
CompleteableFuture word1 = f.thenApply(s -> s + " I am a completable future"); // "Hello world I am a completable futu
有三个按钮:
单击第一个Request HTTP Data As Promise按钮将得到它作为Promise的HTTP响应。
第二个Request HTTP Data As Observable按钮以Observable的形式获得响应。
这两个按钮都使用异步响应机制获得它们的响应。
现在,我希望第三个Request HTTP Data and Wait按钮获得同步响应。我希望它等待http服务返回HTTP响应。
怎么做呢?下面是到Stackblitz项目的链接(请使用在getDataSynchronous脚本中定义的占位符函数getDataSynchronous来实现此功能):
过去两周,我学习了以下内容: setImmediate、process.nextTick、setTimeout、允诺、回调、libuv、事件循环、作业/微任务队列、事件循环队列、调用堆栈等等。
我真的掉进了一个无法逃脱的兔子洞,虽然我发现自己了解得更多了,但我仍然很难在JavaScript中掌握异步代码。
我想使用以下基本场景,并了解如何实现异步。
// does nothing; here to simulate functionality below
var data = new Array(10000000);
const displayTime = desc => {
如果您知道承诺已经解决了,为什么不直接调用get()并接收值呢?而不是在回调函数中使用then(..)。
因此,与其做:
promise.then(function(value) {
// do something with value
});
我想做一个简单得多的事情:
var value = promise.get();
Java为它的提供了这个,我看不出为什么JavaScript不能提供相同的功能。
在测试中,我发现JavaScript承诺始终是异步的,不管它们在链中是否包含任何异步函数。
下面是一些显示控制台中操作顺序的代码。如果您运行它,您将看到即使每个函数都是同步的,输出显示了并行运行的aPromise()调用,以及在运行2完成之前没有发生的"surprisingly this happens after run 2 finishes"。
function aPromise() {
return new Promise(function(resolve, reject) {
console.log("making promise A")
使用Javascript时,请考虑以下情况:
调用中的某个地方的stackSomething变成了一个Promise
发生这种情况的原因有很多。
// a calls b, calls c, and so on.
function z(){
// Let's say this:
// return 4;
// Became this:
return new Promise( (resolve, reject) => resolve(4) );
}
函数Y不能再使用4,因为它被绑定在一个承诺中。幸运的是,函数Y可以await承诺,然后继续正常运行。