Gulp Stream完成承诺

时间:2017-12-05 19:32:21

标签: node.js gulp node-streams

我正在尝试读取文件,执行基于promise的转换并通过流推送结果。这是示例代码。

const gulp = require('gulp')
const gutil = require('gulp-util')
const through2 = require('through2')
const fs = require('fs')

module.exports = (filePath, options) => {

    return gulp.src(filePath)
    .pipe(through2.obj(function (file, enc, cb) {
        const result = file.contents.toString(enc)
        Promise.resolve(result)
        .then( result => {
            cb(null, result)
        })
        .catch(() => {})

    }))

}

由于某种原因,当promise解析并且我尝试调用回调时,我没有得到任何下游,并且流永远不会完成。

我也试过this.push(结果)然后调用回调但仍然是同样的问题。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

所以在搜索了其他gulp util repos后,我发现这是如何正确解决回调的问题:

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np

example_data = np.array([list(range(20))]*4)
cmaplist = ["red", "limegreen", "blue", "gold"]*5

fig, ax = plt.subplots()

norm = colors.BoundaryNorm(np.arange(0,len(cmaplist)+1)-0.5, len(cmaplist))
cmap = colors.LinearSegmentedColormap.from_list('mycmap',cmaplist, len(cmaplist))

im = ax.matshow(example_data, cmap=cmap, norm=norm)

plt.show()