Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 同步使用ImageMagic_Javascript_Node.js_Npm_Imagemagick - Fatal编程技术网

Javascript 同步使用ImageMagic

Javascript 同步使用ImageMagic,javascript,node.js,npm,imagemagick,Javascript,Node.js,Npm,Imagemagick,尝试使用 需要同步使用imagemagick 也就是说,下一个代码只有在图像转换完成后才能执行,无论是错误的还是成功的 我认为只有一种解决方案具有: 如何同步使用imagemagick有什么变体吗?Node.js是单线程的,因此您应该尽量避免使用类似这种同步的函数。您可能只需要在回调函数中执行代码 const ImageMagick = require('imagemagick'); ImageMagick.convert( [ source, path_to ],

尝试使用

需要同步使用imagemagick

也就是说,下一个代码只有在图像转换完成后才能执行,无论是错误的还是成功的

我认为只有一种解决方案具有:


如何同步使用imagemagick有什么变体吗?

Node.js是单线程的,因此您应该尽量避免使用类似这种同步的函数。您可能只需要在回调函数中执行代码

const ImageMagick = require('imagemagick');

ImageMagick.convert(
  [
    source,
    path_to
  ],
  function(err, stdout){
    // the next code performed only after image convertion will be done
});
或者您可以使用Promise和wait,但这样您的整个函数将是异步的

const ImageMagic = require('imagemagick');

function convertImage(source, path_to){
    return new Promise((resolve, reject) => {
        ImageMagick.convert(
            [
                source,
                path_to
            ],
            function(err, stdout){
                if(err) {
                    reject(err)
                }
                resolve(stdout);
            });
    })
}

async function doStuff(){
    // start the image convert
    let stdout = await convertImage(source, path_to);
    // now the function will go on when the promise from convert image is resolved
    // the next code performed only after image convertion will be done
}

Node.js是单线程的,所以您应该尽量避免使用这种同步功能。您可能只需要在回调函数中执行代码

const ImageMagick = require('imagemagick');

ImageMagick.convert(
  [
    source,
    path_to
  ],
  function(err, stdout){
    // the next code performed only after image convertion will be done
});
或者您可以使用Promise和wait,但这样您的整个函数将是异步的

const ImageMagic = require('imagemagick');

function convertImage(source, path_to){
    return new Promise((resolve, reject) => {
        ImageMagick.convert(
            [
                source,
                path_to
            ],
            function(err, stdout){
                if(err) {
                    reject(err)
                }
                resolve(stdout);
            });
    })
}

async function doStuff(){
    // start the image convert
    let stdout = await convertImage(source, path_to);
    // now the function will go on when the promise from convert image is resolved
    // the next code performed only after image convertion will be done
}

node.js的本质是异步的,不需要在node.js中同步执行操作。为什么不将ImageMagick.convert调用包装为承诺并使用async/await?或者答应。那么?@TKoL,这是个好主意。谢谢node.js的本质是异步的,不需要在node.js中同步执行操作。为什么不将ImageMagick.convert调用包装为承诺并使用async/await?或者答应。那么?@TKoL,这是个好主意。谢谢我考虑过了,但在我的例子中,不可能将下一个代码输入回调。你能用更多的细节编辑你的问题吗?为什么你不能这样做呢?那么可能会找到一个解决方案。BraveButter,这里更容易说-我用Express将我的项目从PHP转移到Reactjs。我有一个图像照片加载器,它可以创建许多不同大小的图像。此函数在一个API请求中一致调用多次。因此,我认为从上一个函数调用下一个函数会很麻烦。为async await approch@TKoL NideEdi添加了一个代码示例,我考虑过了,但在我的情况下,无法将下一个代码输入回调。您能否编辑您的问题,并提供一些详细信息,说明您为什么不能这样做,然后可能会找到解决方案,在这里更容易说-我用Express将我的项目从PHP转移到Reactjs。我有一个图像照片加载器,它可以创建许多不同大小的图像。此函数在一个API请求中一致调用多次。所以我认为从前面调用下一个函数会很麻烦