Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

javascript - 不兑现承诺是不好的做法吗?(Is it bad practice to use a promise without reject?)

I have the following;

(我有以下内容;)

function isUserInDatabase(serverID, playerID) {
    return new Promise((resolve, reject) => {
            executeQuery("SELECT * FROM playerdata where serverID=" + serverID + " AND playerID=" + playerID).then((res) => {
                if (res[0] === undefined) {
                    resolve(false);
                } else {
                    resolve(true);
                }
            });
    }).catch ((e) => {
        console.error(e);
        console.log("Error retrieving data from database.");
    });
}

But I have no reject call.

(但是我没有拒绝电话。)

Is this bad convention?

(这是不好的约定吗?)

Edit: I'm honestly not sure if this is better.

(编辑:老实说,我不确定这是否更好。)

I've done a bit more reading into promises, and perhaps this is a little better, but I'm not sure.

(我已经读了一些关于诺言的文章,也许这会更好一些,但是我不确定。)

async function handlePlayer(serverID, playerID) { //TEST
    console.log(await isUserInDatabase(serverID, playerID));
}

function isUserInDatabase(serverID, playerID) {
    return executeQuery("SELECT * FROM playerdata where serverID=? AND playerID=?", [serverID, playerID]).then((res) => {
        if (res[0] === undefined) {
            return false;
        } 
        return true;
    })
    .catch ((err) => {
        console.log(err);
    });
}

async function executeQuery(query, opts) {
    let conn;
    try {
        conn = await pool.getConnection();
        return await conn.query(query, opts);
    } catch (err) {
        console.log(err);
    } finally {
        conn.end();
    }
}
  ask by Kwxhvor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you can use promises without reject.

(好吧,您可以使用承诺而不会拒绝。)

However you must be sure that resolve is called in all your function flows to trigger the "then" handler (or the function must end with an error or exception to trigger the catch clause).

(但是,您必须确保在所有函数流中都调用resolve来触发“ then”处理程序(或者函数必须以错误或异常结尾才能触发catch子句)。)

Reject is often used to trigger error handling logic linked to variable values and results.

(拒绝通常用于触发链接到变量值和结果的错误处理逻辑。)

If the only way your code would fail is due to an exception, then reject is not necessary (this is my opinion though)

(如果您的代码失败的唯一方法是由于异常,那么拒绝是没有必要的(尽管这是我的观点))

As @Jared Smith pointed out, you should do things this way, where the promise actually can reject:

(正如@Jared Smith指出的那样,您应该以这种方式进行操作,在诺言实际上可以拒绝的地方:)

function isUserInDatabase(serverID, playerID) {
    const query = "SELECT * FROM playerdata where serverID=" + serverID + " AND playerID=" + playerID;
    return executeQuery(query).then(res => {
        const isUndefined = res[0] === undefined
        return !isUndefined
    }).catch (e => {
        console.error(e);
        console.log("Error retrieving data from database.");
    });
}

Though you will not be able to handle this promise errors (there's already a catch clause)

(尽管您将无法处理该promise错误(已经有一个catch子句))

You could ignore said catch clause and handle errors after calling isUserInDatabase:

(您可以在调用isUserInDatabase之后忽略上述catch子句并处理错误:)

function isUserInDatabase(serverID, playerID) {
    const query = "SELECT * FROM playerdata where serverID=" + serverID + " AND playerID=" + playerID;
    return executeQuery(query).then(res => {
        const isUndefined = res[0] === undefined
        return !isUndefined;
    });
}

...

isUserInDatabase("some_server","some_player")
.then( ok => console.log("User is in database!") )
.catch( e => {                
    console.error(e);
    console.log("Error retrieving data from database.");
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...