我想更深入地了解Promises如何在内部工作.因此我有一些示例代码:
var p1 = new Promise(
function(resolve, reject) {
window.setTimeout(
function() {
resolve('res called')
}, 2000);
});
var p2 = new Promise(
function(resolve, reject) {
window.setTimeout(
function() {
resolve('res called')
}, 2000);
});
function chainPromises() {
return p1.then(function(val) {
console.log("p1");
return p2.then(function(val) {
console.log("p2");
return val;
});
});
}
chainPromises().then(function(val) {
console.log(val);
});
Run Code Online (Sandbox Code Playgroud)
这是执行此代码的链接.
正如您所预测的那样,首先解决p1,之后是p2,然后最终打印出resolv值.
但API参考声明如下:
var p1 = new Promise(
function(resolve, reject) {
window.setTimeout(
function() {
resolve('res called')
}, 2000);
});
var p2 = new Promise(
function(resolve, …Run Code Online (Sandbox Code Playgroud) 我有两个矩阵:
A = [0,1,1;1,0,0;0,0,0]
B = [3,0,0;0,3,3;4,4,4]
Run Code Online (Sandbox Code Playgroud)
并且我想用矩阵A中的0元素替换,该元素位于矩阵B中的相同位置.
在上面的示例中,结果矩阵如下所示:
result = [3,1,1;1,3,3;4,4,4]
Run Code Online (Sandbox Code Playgroud)
是否有为此目的的matlab功能,或者我必须自己编写一个?
问候
我知道这个话题已经讨论了好几次了,我想我基本上知道数组和指针之间的区别,但是我对数组如何精确地存储在内存中很感兴趣。
例如:
const char **name = {{'a',0},{'b',0},{'c',0},0};
printf("Char: %c\n", name[0][0]); // This does not work
Run Code Online (Sandbox Code Playgroud)
但是,如果这样声明:
const char *name[] = {"a","b","c"};
printf("Char: %c\n", name[0][0]); // Works well
Run Code Online (Sandbox Code Playgroud)
一切正常。