gpt4 book ai didi

javascript - 与 promise 一起工作,做一个储蓄

转载 作者:行者123 更新时间:2023-11-30 16:44:57 25 4
gpt4 key购买 nike

我有以下云函数立即返回, promise 已满,但没有完成它的工作。谁能看出原因?

function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("myClass");

classPromise = (query.find().then
(function(result) {
result[0].set("myField", result[0].get("myField")+1);
result[0].save(null,{}).then
(function() {
console.log("1)newValArray:"+newValArray.length.toString());
newValArray.push(result[0].get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
});
}));

return Parse.Promise.when(classPromise);
}

如果我将此代码用于:

myFunction(newLTR).then
(function() {
console.log("myFunction FULL-FILLED.");
}

我可以在日志中看到消息 “Entered myFunction.”“myFunction FULL-FILLED.”。 但是我从来没有看到 "1)newValArray:..." 我也没有看到 "2)newValArray:..."我还检查了传递的参数没有按预期处理。

如果我将 myFunction 替换为以下版本,则没有任何区别:

 function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;
query = new Parse.Query("Configuration");

classPromise = (query.find().then
(function(result) {
result[0].set("myField", result[0].get("myField")+1);
result[0].save(null,{
success:function(configRcd) {
console.log("1)newValArray:"+newValArray.length.toString());
newValArray.push(configRcd.get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();
},
error:function(error) {
console.log("Something went wrong in incrementLastTouchReference.");
}});
}));

return Parse.Promise.when(classPromise);
}

最佳答案

这是一种糟糕的写 promise 的方式。您首先要使用 promises 的全部原因是您可以链接回调。在您的示例中,这是两个世界中最糟糕的一个,即 promise 的复杂性,但您仍在嵌套。

第二个问题是您确实需要放置一个最终错误处理程序。现在发出的任何错误都可能会消失。 总是catch结尾。

我重写了您的第一个函数以正确 做 promise ,但我不能保证是否没有其他问题。希望它对您有所帮助:

function myFunction(newValArray)
{
console.log("Entered myFunction.");
var query,classPromise;

query = new Parse.Query("myClass");

classPromise = query.find()
.then(function(result) {
result[0].set("myField", result[0].get("myField")+1);
return result[0].save(null,{});
}).then(function() {

console.log("1)newValArray:" + newValArray.length.toString());
newValArray.push(result[0].get("myField"));
console.log("2)newValArray:"+newValArray.length.toString());
return Parse.Promise.as();

}).then(function(result) {

// I added this third then clause. You were returning
// Parse.Promise.as() so presumably you wanted to do something
// with that. Before this then clause it got discarded, with my
// change the result of Parse.Promise.as() is thrown in the
// 'result' argument in this function.

}).catch(function(err) {

// If an error is thrown in any part of the change, it will
// bubble to this final catch statement.
// Do something with err! Log it or whatever ;)

})

return Parse.Promise.when(classPromise);
}

关于javascript - 与 promise 一起工作,做一个储蓄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398200/

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