Javascript 创建一个空的承诺,只是履行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38078203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:03:35  来源:igfitidea点击:

Create an empty promise that just fulfills?

javascriptnode.js

提问by Himmators

I have a wrapper that catches the last result of a promise, formats it and outputs the data:

我有一个包装器,可以捕获承诺的最后一个结果,对其进行格式化并输出数据:

req.resolve = (promise) => {
    return promise.then(() => {
            res.json(req.user);
        }).catch(Sequelize.ValidationError, err => {
        // respond with validation errors
        return res.status(422).send(err.errors);
    }).catch(err => {
        // every other error
        return res.status(400).send({ message: err.message });
    });
};

In one view, I don't have a promise, all that happens is that the auth-function triggers adds req.user and triggers done().

一方面,我没有承诺,所有发生的都是 auth-function triggers 添加 req.user 和 triggers done()

I tried adding a promise like this, but it doesn't get resolved.

我尝试添加这样的承诺,但没有得到解决。

app.get('/user/me', auth,
    (req, res, next) => {
        req.resolve(new Promise());
    });

回答by guest271314

Promiseconstructor requires executor function as parameter. Substitute Promise.resolve()for new Promise()

Promise构造函数需要执行器函数作为参数。替代Promise.resolve()new Promise()