如何向承诺函数传递参数

这似乎是一个愚蠢的问题,但我是一个新手在这个主题。我正在努力实现 Nodejs 的承诺。我想把参数传递给一个承诺函数。然而,我不能弄清楚它。

someModule.someFunction.then(username, password,function(uid) {
/*stuff */
}

函数是这样的

var someFunction = new Promise(username, password, function(resolve, reject) {
/*stuff using username, password*/
if ( /* everything turned out fine */ ) {
resolve("Stuff worked!");
} else {
reject(Error("It broke"));
}
});
181637 次浏览

将你的承诺封装在一个函数中,否则它就会立刻开始工作。另外,还可以向函数传递参数:

var some_function = function (username, password) {
return new Promise(function (resolve, reject) {


/* stuff using username, password */


if (/* everything turned out fine */) {
resolve("Stuff worked!");
} else {
reject(Error("It broke"));
}
});
};

然后,使用它:

some_module.some_function(username, password).then(function (uid) {
// stuff
});

ES6:

const some_function = (username, password) => {
return new Promise((resolve, reject) => {


/* stuff using username, password */


if (/* everything turned out fine */) {
resolve("Stuff worked!");
} else {
reject(Error("It broke"));
}
});
};

用途:

some_module.some_function(username, password).then((uid) => {
// stuff
});

另一种方式(必须尝试) :

var promise1 = new Promise(function(resolve, reject) {
resolve('Success!');
});
var extraData = 'ImExtraData';
promise1.then(function(value) {
console.log(value, extraData);
// expected output: "Success!" "ImExtraData"
}, extraData);

可以使用. bind ()将参数(这个)传递给函数。

var someFunction =function(resolve, reject) {
/* get username, password*/
var username=this.username;
var password=this.password;
if ( /* everything turned out fine */ ) {
resolve("Stuff worked!");
} else {
reject(Error("It broke"));
}
}
var promise=new Promise(someFunction.bind({username:"your username",password:"your password"}));

甚至更短

var foo = (user, pass) =>
new Promise((resolve, reject) => {
if (/* condition */) {
resolve("Fine");
} else {
reject("Error message");
}
});


foo(user, pass).then(result => {
/* process */
});

你可以在一个带参数的函数中返回你的承诺,像这样:

function someFunction(username, password) {
return new Promise((resolve, reject) => {
// Do something with the params username and password...
if ( /* everything turned out fine */ ) {
resolve("Stuff worked!");
} else {
reject(Error("It didn't work!"));
}
});
}
    

someFunction(username, password)
.then((result) => {
// Do something...
})
.catch((err) => {
// Handle the error...
});

我用这个。

let verifyEmail = (email) => new Promise((resolve, rejected) => {
let rsp = {isRepeated:false}
let sql = `select id from users where email='${email}' `
try{
db.query(sql, (err,result)=>{
if(!err){
if(result.length > 0){
rsp = {isRepeated:true}
}
}
})
resolve(rsp)
}catch(e){
rejected({ok:false,err:e})
}
})

我知道这很古老,但这可能会帮助人们在谷歌上找到答案。这是一个更实际的例子,我只需要将2个参数传递给一个承诺函数,然后等待它解析。希望这能帮上忙。SetTimeout 模拟“处理时间”。3秒后,该值将从承诺返回并打印到屏幕上。

const addNumbers = (a, b) => {
    

return new Promise((resolve, reject) => {
    

setTimeout(() => {
resolve(a + b);
}, 3000);
  

});
  

  

};


let getResult = async (a, b) => {
let value = await addNumbers(a, b);
console.log(value);
};


getResult(1, 3);

不知道是否理解正确,但是为了将参数从承诺发送到另一个函数,你可以这样做:

function logUser(username, password){
return new Promise((resolve, reject) => {
let response = 'user logged in'
resolve(response)
})
}


function showMessage(msg){
console.log(msg)
}


logUser('user', 'pass').then((respFromLogUser) => {
showMessage(respFromLogUser)
})