我已经在几个地方读过有关承诺处理器模式,但我无法弄清楚它是什么.有人建议我在代码中使用它,看起来像:
function getDb(){
return myDbDriver.getConnection();
}
var users = getDb().then(function(conn){
return conn.query("SELECT name FROM users").finally(function(users){
conn.release();
});
});
Run Code Online (Sandbox Code Playgroud)
什么是承诺处理器模式以及它如何应用于此?
注意 - 在原生承诺中,我.finally称之为"添加拒绝和履行处理程序,返回值但执行操作".如果重要的话,我在这种情况下使用蓝鸟.
NodeJS 0.11以及io.js和Node 0.12分支都附带本机承诺.
本机承诺有一个.then总是在未来事件循环周期上执行的方法.
到目前为止setImmediate,自从我从nextTick切换以来,我一直在使用事件循环到事件循环的下一次迭代:
setImmediate(deferThisToNextTick); // My NodeJS 0.10 code
process.nextTick(deferThisToNextTick); // My NodeJS 0.8 code
Run Code Online (Sandbox Code Playgroud)
因为我们现在有了一种新方法:
Promise.resolve().then(deferThisToNextTick);
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个?此外 - 对于在事件循环之前或之后运行的代码,它的Promise.resolve.then作用是什么setImmediate样的nextTick?
如何更改以下代码,以便触发异步操作并同时运行?
const value1 = await getValue1Async();
const value2 = await getValue2Async();
// use both values
Run Code Online (Sandbox Code Playgroud)
我需要做这样的事情吗?
const p1 = getValue1Async();
const p2 = getValue2Async();
const value1 = await p1;
const value2 = await p2;
// use both values
Run Code Online (Sandbox Code Playgroud) 我在这里读到了很多关于"按值"和"按引用"传递以将数组发送到javascript函数的答案.然而,我在向函数发送数组并使原始数组保持不变时遇到问题.这个例子说明了这个问题:
function myFunction(someArray)
{
// any function that makes an array based on a passed array;
// someArray has two dimensions;
// I've tried copying the passed array to a new array like this (I've also used 'someArray' directly in the code);
funcArray = new Array();
funcArray = someArray;
var i = 0;
for(i=0; i<funcArray.length; i++)
{
funcArray[i].reverse;
}
return funcArray;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么这个函数中的任何东西都应该改变原始数组.
如果将函数调用分配给新数组,则调用此函数会直接更改原始数组:
myArray = [["A","B","C"],["D","E","F"],["G","H","I"]];
anotherArray = new Array();
anotherArray = myFunction(myArray);
// myArray gets modified!;
Run Code Online (Sandbox Code Playgroud)
我尝试使用.valueOf()发送原语:
anotherArray …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用新的(ES6)Map对象来表示属性和值之间的映射.
我有类似于以下形式的对象:
{key1:value1_1,key2:value2_1},..... {key1:value1_N,key2:value2_N}
Run Code Online (Sandbox Code Playgroud)
我想根据key1 和 key2值对它们进行分组.
举例来说,我希望能够以组以下的x和y:
[{x:3,y:5,z:3},{x:3,y:4,z:4},{x:3,y:4,z:7},{x:3,y:1,z:1},{x:3,y:5,z:4}]
Run Code Online (Sandbox Code Playgroud)
并获取包含以下内容的Map:
{x:3,y:5} ==> {x:3,y:5,z:3},{x:3,y:5,z:4}
{x:3,y:4} ==> {x:3,y:4,z:4},{x:3,y:4,z:7}
{x:3,y:1} ==> {x:3,y:1,z:1}
Run Code Online (Sandbox Code Playgroud)
在Python中,我使用元组作为字典键.ES6映射允许任意对象作为键,但使用标准相等算法(===),因此对象只能通过引用来区分我所知道的.
如何使用ES6地图完成这种分组?或者,如果有一种我忽略的优雅方式,使用普通JS对象的解决方案.
我宁愿不使用外部集合库 - 但如果有一个更好的解决方案使用我也有兴趣了解它.
在尝试捕获承诺拒绝时,我在IE8上遇到了一个奇怪的错误(基本ngResource调用返回的承诺):
此代码使用.then(success, fail)语法:
promise.then(function(response) {
// success
},
function(response) {
// error
});
Run Code Online (Sandbox Code Playgroud)
但这个失败的.then(success).catch(fail)语法:
promise.then(function(response) {
// success
})
.catch(function(response) {
// error
});
Run Code Online (Sandbox Code Playgroud)
并且指向该.catch()行的IE错误是:
预期的标识符
难道我做错了什么 ?有人重现了吗?或者由于受限制的关键字,它是一个常见的IE8?
谢谢
internet-explorer internet-explorer-8 promise angularjs angular-promise
如果旧文件足够老,我需要我的脚本来下载新文件.我以秒为单位设置文件的最大年龄.因此,我将使用我的脚本编写回到正轨,我需要示例代码,其中文件年龄以秒为单位打印出来.
有没有人知道,使用Modernizr或其他方法,如果有办法检测是否在浏览器中启用了Promise功能?
我有一个polyfill用于功能,但只想在浏览器没有本机实现时应用它.
我希望能够在C++中将数组声明为函数参数,如下面的示例代码所示(不编译).有没有办法做到这一点(除了预先单独声明数组)?
#include <stdio.h>
static void PrintArray(int arrayLen, const int * array)
{
for (int i=0; i<arrayLen; i++) printf("%i -> %i\n", i, array[i]);
}
int main(int, char **)
{
PrintArray(5, {5,6,7,8,9} ); // doesn't compile
return 0;
}
Run Code Online (Sandbox Code Playgroud) 的JSON标准以一种方式定义的目的和ECMAScript的(JavaScript的)标准定义它在另一个.
通常说JSON对象是JavaScript对象的子集,这是真的吗?
每个JSON对象都是有效的JavaScript对象吗?