gpt4 book ai didi

javascript - 从 promise 链内部解决问题安全吗?

转载 作者:搜寻专家 更新时间:2023-11-01 00:23:30 24 4
gpt4 key购买 nike

<分区>

恐怕我所做的是完全错误的。

IN promise 链中使用 resolve() 或 reject() 是否可以。

// Insert a new user by details into database
insert: function(username, name, email, gender, birthday, other) {
return new Promise(function(resolve, reject) {

// Base structure
var new_user = {
username: username,
name: name,
email: email,
gender: gender,
birthday: birthday
};

// Merge other data with base structure
if(other != undefined) {
new_user = _.merge(new_user, other);
}

var new_user_model = new UserModel(new_user);
new_user_model.saveAsync(function(user) {
return resolve(user); // Safe to resolve from inside a promise?
});

// OR... should I do it like this?
var user = new_user_model.saveAsync(function(user) {
return user;
});
return resolve(user);
});
}

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