使用 bluebird 和 coffeescript 的简单承诺示例工作了一半
Simple promise example with bluebird and coffeescript works half the time
所以我基本上是在尝试使用 promises 编写一些简单的代码,但我在理解为什么这段特定代码每隔一段时间都有效时遇到了一些麻烦。
Promise = require('bluebird')
mkdirp = Promise.promisify(require('mkdirp'))
rm = Promise.promisify(require('rimraf'))
console.log "Preparing build directory"
rm('build')
.then(mkdirp('build'))
这将成功完成第一个 运行,但第二个将失败,依此类推。
步骤如下:
┌[adam@bigboi] [/dev/pts/5] [master ⚡]
└[~/Projects/bummyjab]> time coffee index.coffee ~/Dropbox/Articles/*.md
Preparing build directory
coffee index.coffee ~/Dropbox/Articles/*.md 0.25s user 0.02s system 100% cpu 0.267 total
┌[adam@bigboi] [/dev/pts/5] [master ⚡]
└[~/Projects/bummyjab]> stat build
File: ‘build’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 804h/2052d Inode: 17172395 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1000/ adam) Gid: ( 1000/ adam)
Access: 2015-06-25 22:07:49.061331341 -0400
Modify: 2015-06-25 22:07:49.061331341 -0400
Change: 2015-06-25 22:07:49.061331341 -0400
Birth: -
┌[adam@bigboi] [/dev/pts/5] [master ⚡]
└[~/Projects/bummyjab]> time coffee index.coffee ~/Dropbox/Articles/*.md
Preparing build directory
Unhandled rejection Error: EEXIST: file already exists, mkdir '/home/adam/Projects/bummyjab/build'
at Error (native)
coffee index.coffee ~/Dropbox/Articles/*.md 0.20s user 0.03s system 100% cpu 0.235 total
不幸的是,我的 google 技能还没有找到发生这种情况的原因。
谢谢
如果您试图控制顺序以便 mkdirp('build')
仅在 rm('build')
完成后发生,那么您需要将函数引用传递给 .then()
,如下所示:
rm('build').then(function () {
return mkdirp('build');
});
或者,您可以使用 .bind()
:
rm('build').then(mkdirp.bind(null, 'build'));
您正在做的是立即执行 mkdirp()
并将该 return 值传递给 .then()
,它不会等到 promise 得到解决才执行它。
所以我基本上是在尝试使用 promises 编写一些简单的代码,但我在理解为什么这段特定代码每隔一段时间都有效时遇到了一些麻烦。
Promise = require('bluebird')
mkdirp = Promise.promisify(require('mkdirp'))
rm = Promise.promisify(require('rimraf'))
console.log "Preparing build directory"
rm('build')
.then(mkdirp('build'))
这将成功完成第一个 运行,但第二个将失败,依此类推。
步骤如下:
┌[adam@bigboi] [/dev/pts/5] [master ⚡]
└[~/Projects/bummyjab]> time coffee index.coffee ~/Dropbox/Articles/*.md
Preparing build directory
coffee index.coffee ~/Dropbox/Articles/*.md 0.25s user 0.02s system 100% cpu 0.267 total
┌[adam@bigboi] [/dev/pts/5] [master ⚡]
└[~/Projects/bummyjab]> stat build
File: ‘build’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 804h/2052d Inode: 17172395 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1000/ adam) Gid: ( 1000/ adam)
Access: 2015-06-25 22:07:49.061331341 -0400
Modify: 2015-06-25 22:07:49.061331341 -0400
Change: 2015-06-25 22:07:49.061331341 -0400
Birth: -
┌[adam@bigboi] [/dev/pts/5] [master ⚡]
└[~/Projects/bummyjab]> time coffee index.coffee ~/Dropbox/Articles/*.md
Preparing build directory
Unhandled rejection Error: EEXIST: file already exists, mkdir '/home/adam/Projects/bummyjab/build'
at Error (native)
coffee index.coffee ~/Dropbox/Articles/*.md 0.20s user 0.03s system 100% cpu 0.235 total
不幸的是,我的 google 技能还没有找到发生这种情况的原因。
谢谢
如果您试图控制顺序以便 mkdirp('build')
仅在 rm('build')
完成后发生,那么您需要将函数引用传递给 .then()
,如下所示:
rm('build').then(function () {
return mkdirp('build');
});
或者,您可以使用 .bind()
:
rm('build').then(mkdirp.bind(null, 'build'));
您正在做的是立即执行 mkdirp()
并将该 return 值传递给 .then()
,它不会等到 promise 得到解决才执行它。