承诺不返回回调数组

Promisify to not return callback array

本文关键字:数组 回调 返回 承诺      更新时间:2023-09-26

有什么方法可以增强回调以仅返回result而不是包含raw的数组?

var Promise = require("bluebird");
var osascript = require('node-osascript');
function osxBackgroundGet(callback) {
  return osascript.execute([
    'tell application "System Events"',
    'tell current desktop',
    'get picture',
    'end tell',
    'end tell'
  ].join("'n"), callback);
}
/*
osxBackgroundGet(function(err, result, raw){
  console.log(result);
})
*/
var osxBackgroundGet = Promise.promisify(osxBackgroundGet);
osxBackgroundGet().then(function(signature){
  var result = signature[0];
})

还是我应该在osxBackgroundGet callback内处理这个问题?

喜欢这个:

function osxBackgroundGet(callback) {
  return osascript.execute([
    'tell application "System Events"',
    'tell current desktop',
    'get picture',
    'end tell',
    'end tell'
  ].join("'n"), function(err, result, raw){
    if(err) return callback(err)
    return callback(err, result)
  });
}

我真的不喜欢编辑图书馆的原生callback。我应该离开signature数组吗?

如果我只是promisifyAll osascript,我可以在这里削减很多步骤.但我仍然只剩下那个的signature array.

var Promise = require("bluebird")
var osascript = Promise.promisifyAll(require('node-osascript'))
osascript.executeAsync([
  'tell application "System Events"',
  'tell current desktop',
  'get picture',
  'end tell',
  'end tell'
].join("'n")).then(function(signature){
  console.log(signature);
});

更新:

我可以通过 4 种方式编写/使用这些函数,我对哪个是"标准"感到困惑,或者我想主观的"最佳"有些提供更多的多功能性,有些则更容易理解和工作如您所期望的那样。

var Promise = require("bluebird")
var osascript = require('node-osascript');
var osascriptPromise = Promise.promisifyAll(osascript)

选项 1

返回

回调,只返回整个本机回调。

function osxBackgroundGetCallback(callback){
  return osascript.execute([
    'tell application "System Events"',
    'tell current desktop',
    'get picture',
    'end tell',
    'end tell'
  ].join("'n"), callback);
}
// usage
/*
var getBg = Promise.promisify(osxBackgroundGetCallback);
getBg().then(function(sig){
  var bg = sig[0]
});
*/

选项 2

返回

回调并打开回调以返回特定值。

function osxBackgroundGetCallbackValue(callback){
  return osascript.execute([
    'tell application "System Events"',
    'tell current desktop',
    'get picture',
    'end tell',
    'end tell'
  ].join("'n"), function(err, result, raw){
    if(err) return callback(err);
    return callback(result);
  });
}
// usage
/*
var getBg = Promise.promisify(osxBackgroundGetCallbackValue);
getBg().then(function(bg){
});
*/

选项 3

返回带有本机回调签名数组的承诺(没有错误)。

function osxBackgroundGetPromise(){
  return osascriptPromise.executeAsync([
    'tell application "System Events"',
    'tell current desktop',
    'get picture',
    'end tell',
    'end tell'
  ].join("'n"))
}
// usage
/*
getBg().then(function(sig){
  var bg = sig[0];
});
*/

选项 4

返回

承诺,然后返回所需的特定值。

function osxBackgroundGetPromiseValue(){
  return osascriptPromise.executeAsync([
    'tell application "System Events"',
    'tell current desktop',
    'get picture',
    'end tell',
    'end tell'
  ].join("'n")).then(function(sig){
    var result = sig[0]
    var raw = sig[1]
    return result
  })
}
// usage
/*
getBg().then(function(bg){
});
*/

最好的选择是选项 1/3,但不是使用 then 使用 spread .这样您就可以访问所有变量,而无需深入研究雄心勃勃的数组。