Promise 不在外部执行,但它在函数内部
Promise not executing then outside but it is inside the function
我正在使用承诺在 React 中分派异步操作。我只想更改对象的属性。该功能有效。如果我将代码包装在一个承诺中并且在执行 Promise().then
之后工作但是如果我执行 myFunction(params).then()
则 then is not called
这里是我的功能。 (它是递归的):
export function setValue(propertyPath, value, obj) {
console.log("setting the value")
return new Promise((resolve, reject) => {
// this is a super simple parsing, you will want to make this more complex to handle correctly any path
// it will split by the dots at first and then simply pass along the array (on next iterations)
let properties = Array.isArray(propertyPath) ? propertyPath : propertyPath.split(".")
// Not yet at the last property so keep digging
if (properties.length > 1) {
// The property doesn't exists OR is not an object (and so we overwrite it) so we create it
if (!obj.hasOwnProperty(properties[0]) || typeof obj[properties[0]] !== "object") obj[properties[0]] = {}
// We iterate.
return setValue(properties.slice(1), value, obj[properties[0]])
// This is the last property - the one where to set the value
} else {
// We set the value to the last property
obj[properties[0]] = value
console.log("modified object")
console.log(obj)
return resolve(obj)
}
}
)
}
export function performPocoChange(propertyPath, value, obj) {
console.log("we are indeed here")
return (dispatch) => {
setValue(propertyPath, value, obj).then((poco) => {
console.log("poco is here")
console.log(poco)
dispatch(changePoco(poco))
})
}
}
控制台显示 "we are indeed here" 但不显示 "Poco is here" 并且未调用 changePoco(即操作)。但是,如果我在承诺的括号后立即执行 then
并记录它会显示日志。
我不是 JS 专家。我真的很努力,这真的让我着迷。有什么想法吗?
非常感谢
除了 else
之外,您没有 resolve
承诺,因此 then 不会被执行。
我正在使用承诺在 React 中分派异步操作。我只想更改对象的属性。该功能有效。如果我将代码包装在一个承诺中并且在执行 Promise().then
之后工作但是如果我执行 myFunction(params).then()
则 then is not called
这里是我的功能。 (它是递归的):
export function setValue(propertyPath, value, obj) {
console.log("setting the value")
return new Promise((resolve, reject) => {
// this is a super simple parsing, you will want to make this more complex to handle correctly any path
// it will split by the dots at first and then simply pass along the array (on next iterations)
let properties = Array.isArray(propertyPath) ? propertyPath : propertyPath.split(".")
// Not yet at the last property so keep digging
if (properties.length > 1) {
// The property doesn't exists OR is not an object (and so we overwrite it) so we create it
if (!obj.hasOwnProperty(properties[0]) || typeof obj[properties[0]] !== "object") obj[properties[0]] = {}
// We iterate.
return setValue(properties.slice(1), value, obj[properties[0]])
// This is the last property - the one where to set the value
} else {
// We set the value to the last property
obj[properties[0]] = value
console.log("modified object")
console.log(obj)
return resolve(obj)
}
}
)
}
export function performPocoChange(propertyPath, value, obj) {
console.log("we are indeed here")
return (dispatch) => {
setValue(propertyPath, value, obj).then((poco) => {
console.log("poco is here")
console.log(poco)
dispatch(changePoco(poco))
})
}
}
控制台显示 "we are indeed here" 但不显示 "Poco is here" 并且未调用 changePoco(即操作)。但是,如果我在承诺的括号后立即执行 then
并记录它会显示日志。
我不是 JS 专家。我真的很努力,这真的让我着迷。有什么想法吗?
非常感谢
除了 else
之外,您没有 resolve
承诺,因此 then 不会被执行。