问:Promisify同步操作链接承诺?

时间:2022-05-30 11:41:00

Is there any merit in promisifying synchronous operations so that by design they can be chained in onSuccess or onError callbacks?

在同步操作中是否有任何优点,以便通过设计将它们链接在onSuccess或onError回调中?

Eg:

function loadSettings(path) {
    if (fs.existsSync(path)) {
        return Q(fsJson.loadSync(path));
    }
    return new Q.defer().reject('No local settings!');
}

doingSomethingFirst()
    .then(loadSettings, obtainSettings)
    .then(doSomethingWithSettings)
    .done()

What's best?

2 个解决方案

#1


No, moreover, it gives the false impression that these methods are async so you or other developers might call them and expect that the method is not undermining the entire io.js/node.js concurrency model by performing sync IO.

不,此外,它给人以错误的印象,即这些方法是异步的,因此您或其他开发人员可能会调用它们,并期望该方法不会通过执行同步IO来破坏整个io.js / node.js并发模型。

I recommend that you either make these functions not return promises or make them perform async IO. Also note that your method has a race condition (what if the file is deleted between when you check it exists and when you try to access it?)

我建议您使这些函数不返回promises或使它们执行异步IO。另请注意,您的方法具有竞争条件(如果在检查文件时以及尝试访问文件时删除文件,该怎么办?)

#2


Actually this particular chain will literally work exactly the same even if you wrote loadSettings like this:

实际上,即使你编写了像这样的loadSettings,这个特定的链也会完全相同:

function loadSettings(path) {
    if (fs.existsSync(path)) {
        return fsJson.loadSync(path);
    }
    throw 'No local settings!';
}

Note that it's a horrible practice to reject with strings or throw strings so ideally you'd want new Error('No local settings!') instead. I mean just imagine if that error actually happened and it was just a string - you would have no idea how or where the error really happened.

请注意,拒绝使用字符串或抛出字符串是一种可怕的做法,因此理想情况下,您需要新的错误(“无本地设置!”)。我的意思是想象一下,如果该错误实际发生并且它只是一个字符串 - 您将不知道错误是如何或在何处发生的。

#1


No, moreover, it gives the false impression that these methods are async so you or other developers might call them and expect that the method is not undermining the entire io.js/node.js concurrency model by performing sync IO.

不,此外,它给人以错误的印象,即这些方法是异步的,因此您或其他开发人员可能会调用它们,并期望该方法不会通过执行同步IO来破坏整个io.js / node.js并发模型。

I recommend that you either make these functions not return promises or make them perform async IO. Also note that your method has a race condition (what if the file is deleted between when you check it exists and when you try to access it?)

我建议您使这些函数不返回promises或使它们执行异步IO。另请注意,您的方法具有竞争条件(如果在检查文件时以及尝试访问文件时删除文件,该怎么办?)

#2


Actually this particular chain will literally work exactly the same even if you wrote loadSettings like this:

实际上,即使你编写了像这样的loadSettings,这个特定的链也会完全相同:

function loadSettings(path) {
    if (fs.existsSync(path)) {
        return fsJson.loadSync(path);
    }
    throw 'No local settings!';
}

Note that it's a horrible practice to reject with strings or throw strings so ideally you'd want new Error('No local settings!') instead. I mean just imagine if that error actually happened and it was just a string - you would have no idea how or where the error really happened.

请注意,拒绝使用字符串或抛出字符串是一种可怕的做法,因此理想情况下,您需要新的错误(“无本地设置!”)。我的意思是想象一下,如果该错误实际发生并且它只是一个字符串 - 您将不知道错误是如何或在何处发生的。