gpt4 book ai didi

javascript - Promise 扩展到更多的 Promise 会导致不可预测的数据格式(以及不可读的代码)

转载 作者:行者123 更新时间:2023-12-02 16:51:32 25 4
gpt4 key购买 nike

我正在尝试使用 Promise 重写一些“旧”代码。目前,代码使用自己的堆栈,可以从所有回调访问该堆栈,有时将新项目添加到堆栈中,直到堆栈为空,然后运行最终的回调,以所有收集的数据的漂亮格式(也可以在所有回调中访问)回调)。

这里的要点是:stackdata可以从所有回调中访问,因此每个回调都可以使用添加到data >推。最终结果始终是一维数组。

  1. 该过程始终以 Promise.all([a, b])
  2. 开始
  3. a 添加到 data 即可
  4. b 可能会添加到 data 中,仅此而已,更有可能产生更多的 Promise
  5. 这些 promise 添加到数据和/或产生更多 promise
  6. 等等,理论上是无限的,但通常只有 1-3 层深

对于 b 中的每个新生成,之前的结果都是必需的,因此我无法使用 ab 一起启动它们.

我做了一个简化的演示:http://jsfiddle.net/rudiedirkx/6r5smkz5/ (检查控制台并点击空白主体再次运行)

  • 有时b返回标量(立即数据)
  • 有时它会返回来自更多生成点的数据(数组)
  • 这些生成还可能从更多生成(更多数组!)返回数据

因此数据可能如下所示:

  • [a, b]
  • [a, [b, b]]
  • [a, [b, [c, c, c], [c, c]]](演示中没有,只有 2 个级别)

等等

它应该总是这样:

  • [a, b, c, c, c, c, c](1D,任意数量的元素)

在每个 then(callback) 中,我只有来自 P.all 的结果,而不是全部,直到最后一次完成,其中格式可以是任何东西。

这是处理器:

console.time('ALL LOADED');
Promise.all([local(), sync()])
.then(function(data) {
// data[0] (from a) is always scalar
// data[1] (from 1 or more b's) might be Array
if (data[1] instanceof Array) {
data[1].unshift(data[0]);
return data[1];
}
return data;
})
.then(function(data) {
console.log('DONE', data);
console.timeEnd('ALL LOADED');
console.log('');
});

如您所见,第一个 then(callback) 将数据格式化为我需要的一维数组。这个很简单,但是级别越多,规模就越大。

我的问题主要是:这是正确的方法吗?不是有一个“在每个then(callback)”之后可以轻松地格式化数据吗?或者甚至更好:更容易访问全局数据,就像我当前的设置一样。

或者在第一个 P.all 堆栈仍在运行时添加到它?那是最好的。只有 1 个 then(callback) 和一个 data,但大小可变。

promise 很酷,但复杂的事情仍然很复杂,甚至更复杂。

最佳答案

我之前没有尝试过在递归中使用 Promise,所以我稍微搞了一下。我不确定这对您是否有帮助,但这对我来说很有趣。

我仍在努力不使用 globalData 变量作为累加器,但是在 promise 中传递累加器会有点奇怪,我还不知道为什么。

var prom = require('bluebird'),
_ = require('lodash'),
globalData = []

/**
* return a number 1/2 of the time, array rest of the time
*/
function asyncSometimesArrayDataSource() {
if (Math.random() > 0.5) {
return prom.resolve(2);
} else {
return prom.resolve([1, 2]);
}
};


function asyncAlwaysIntDataSource() {
return prom.resolve(2);
}


function recursivePromises(arr) {
return prom.all(asyncSometimesArrayDataSource()
.then(function (data) {
if (data instanceof Array) {
return prom.all(data.map(function (val) {

globalData.push(val)
return recursivePromises();
}));
} else {
globalData.push(data);
return globalData;
}
}));

}

asyncAlwaysIntDataSource()
.then(function (easyData) {
return recursivePromises()
.then(function () {
return globalData;
});
})
.then(function (finaldata) {
console.log('final data', finaldata);
});

这是之前尝试的改进版本,没有外部累积数组。

var prom = require('bluebird'),
_ = require('lodash'),
// count is used to verify that the final array is the correct length
count = 0;


/**
* return a number 1/2 of the time, array rest of the time
*/
function asyncSometimesArrayDataSource() {
if (Math.random() > 0.5) {
return prom.resolve(2);
} else {
return prom.resolve([4, 5]);
}
}


function asyncAlwaysIntDataSource() {
count++;
return prom.resolve(2);
}



function recursivePromises(arr) {
return asyncSometimesArrayDataSource()
.then(function (data) {
if (data instanceof Array) {
return prom.map(data, function (val) {
count++;
return recursivePromises([val]);
}).then(function (mapdata) {
return arr.concat(mapdata);
});
} else {
count++;
arr.push(data);
return arr;
}
});
}

asyncAlwaysIntDataSource()
.then(function (easyData) {
return recursivePromises([easyData])
})
.then(function (finaldata) {
console.log('final data', _.flatten(finaldata), count, _.flatten(finaldata).length);
});

关于javascript - Promise 扩展到更多的 Promise 会导致不可预测的数据格式(以及不可读的代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com