我编写的代码看起来像:
function getStuffDone(param) { | function getStuffDone(param) {
var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) {
// or = new $.Deferred() etc. | // using a promise constructor
myPromiseFn(param+1) | myPromiseFn(param+1)
.then(function(val) { /* or .done */ | .then(function(val) {
d.resolve(val); | resolve(val);
}).catch(function(err) { /* .fail */ | }).catch(function(err) {
d.reject(err); | reject(err);
}); | });
return d.promise; /* or promise() */ | });
} | }
Run Code Online (Sandbox Code Playgroud)
有人告诉我这个被称为" 延迟反模式 "或" Promise构造函数反模式 ",这个代码有什么不好,为什么这被称为 …
我正在为个人需求开发类似脚本的控制台.我需要能够暂停一段时间,但是,根据我的研究,node.js无法按要求停止.经过一段时间后很难读取用户的信息......我已经看到了一些代码,但我相信他们必须在其中包含其他代码才能使其工作,例如:
setTimeout(function() {
}, 3000);
Run Code Online (Sandbox Code Playgroud)
但是,在这段代码之后,我需要在一段时间后执行所有内容.
例如,
//start-of-code
console.log('Welcome to My Console,');
some-wait-code-here-for-ten-seconds..........
console.log('Blah blah blah blah extra-blah');
//endcode.
Run Code Online (Sandbox Code Playgroud)
我也看过像
yield sleep(2000);
Run Code Online (Sandbox Code Playgroud)
但node.js不承认这一点.
我怎样才能实现这种延长的暂停?
请考虑以下以串行/顺序方式读取文件数组的代码.readFiles返回一个promise,只有在按顺序读取所有文件后才会解析.
var readFile = function(file) {
... // Returns a promise.
};
var readFiles = function(files) {
return new Promise((resolve, reject) =>
var readSequential = function(index) {
if (index >= files.length) {
resolve();
} else {
readFile(files[index]).then(function() {
readSequential(index + 1);
}).catch(reject);
}
};
readSequential(0); // Start!
});
};
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但我不喜欢按顺序进行递归递归.是否有一种更简单的方法可以重写此代码,以便我不必使用我的奇怪readSequential功能?
最初我试图使用Promise.all,但这导致所有readFile调用同时发生,这不是我想要的:
var readFiles = function(files) {
return Promise.all(files.map(function(file) {
return readFile(file);
}));
};
Run Code Online (Sandbox Code Playgroud) Lint错误消息:
src/app/detail/edit/edit.component.ts [111,5]:for(... in ...)语句必须用if语句过滤
代码片段(这是一个工作代码.它也可以在angular.io表单验证部分获得):
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道如何修复这个lint错误吗?
看看MDN,看起来values传递给then()Promise.all 的回调包含了promises顺序的值.例如:
var somePromises = [1, 2, 3, 4, 5].map(Promise.resolve);
return Promise.all(somePromises).then(function(results) {
console.log(results) // is [1, 2, 3, 4, 5] the guaranteed result?
});
Run Code Online (Sandbox Code Playgroud)
任何人都可以引用规范说明values应该在哪个顺序?
PS:运行这样的代码表明这似乎是真的,虽然这当然没有证据 - 它可能是巧合.
我写了这段代码 lib/helper.js
var myfunction = async function(x,y) {
....
reutrn [variableA, variableB]
}
exports.myfunction = myfunction;
Run Code Online (Sandbox Code Playgroud)
然后我试着在另一个文件中使用它
var helper = require('./helper.js');
var start = function(a,b){
....
const result = await helper.myfunction('test','test');
}
exports.start = start;
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误
"await仅在异步函数中有效"
有什么问题?
似乎有一些问题将async/await与.reduce()结合起来,如下所示:
const data = await bodies.reduce(async(accum, current, index) => {
const methodName = methods[index]
const method = this[methodName]
if (methodName == 'foo') {
current.cover = await this.store(current.cover, id)
console.log(current)
return {
...accum,
...current
}
}
return {
...accum,
...method(current.data)
}
}, {})
console.log(data)
Run Code Online (Sandbox Code Playgroud)
该data对象被记录之前的this.store完成...
我知道你可以使用Promise.all异步循环,但这适用于.reduce()?
我已经打开了Chrome标志,用于实验性的ECMAscript 6功能,其中之一就是Set.据我了解,详细信息Set由规范编写者广泛认同.
我创建一个集合a并添加字符串'Hello'
a = Set();
a.add('Hello');
Run Code Online (Sandbox Code Playgroud)
但我如何迭代元素a?
for(let i of a) { console.log(i); }
Run Code Online (Sandbox Code Playgroud)
给出"SyntaxError:let扩展模式外的非法声明"
for(var i of a) { console.log(i); }
Run Code Online (Sandbox Code Playgroud)
给出"SyntaxError:意外的标识符"
for(var i in a) { console.log(i); }
Run Code Online (Sandbox Code Playgroud)
给 Undefined
是否有可能在Chrome 26中迭代一组?
这有什么区别吗:
const promises = await Promise.all(items.map(e => somethingAsync(e)));
for (const res of promises) {
// do some calculations
}
Run Code Online (Sandbox Code Playgroud)
和这个 ?
for await (const res of items.map(e => somethingAsync(e))) {
// do some calculations
}
Run Code Online (Sandbox Code Playgroud)
我知道在第一个片段中,所有的承诺都被同时触发,但我不确定第二个。for 循环是否等待第一次迭代完成以调用下一个 promise ?还是所有的 Promise 都是同时触发的,循环内部就像是它们的回调?
此代码生成错误:
function *giveNumbers() {
[1, 2, 3].forEach(function(item) {
yield item;
})
}
Run Code Online (Sandbox Code Playgroud)
这可能是因为yield在一个不是生成器的函数内部.有一种优雅的方法来克服这个问题吗?我的意思是:
function *giveNumbers() {
let list = [1, 2, 3];
for (let i = 0; i < list.length; i++) {
yield list[i];
}
}
Run Code Online (Sandbox Code Playgroud) javascript ×9
promise ×5
node.js ×4
async-await ×2
ecmascript-6 ×2
es6-promise ×2
q ×2
angular ×1
angular-cli ×1
bluebird ×1
for-await ×1
reduce ×1
sequential ×1
tslint ×1
yield ×1