Promises.all()在服务器响应之前未解析

时间:2018-10-26 04:52:58

标签: google-cloud-firestore google-cloud-functions es6-promise google-cloud-dataflow

我正在云功能内启动google dataflow模板作业。该函数应返回作为单个响应启动的每个作业的结果。

职位描述存储在Firestore数据库中。

作业正常启动,我的问题是从云功能(服务器)返回Job响应。

app.get('/', (req, res) => {

    var dataJob = {};

    var workflow = fs.collection('dataprep-workflows').doc('18c').get()
        .then((workflow) => {

            if (!workflow.exists) throw (new Error('no workflow.'));

            dataJob = workflow.data();

            return workflow.ref.collection('dataflow-jobs').get();
        })
        .then(async (jobs) => {

            if (jobs.empty) throw (new Error('no jobs'));

            const authClient = await _authenticateWithGoogle();

            const dataJobs = jobs.docs.map((job) => {
                var o = JSON.parse(JSON.stringify(dataJob));
                o.jobs = [job.data()];
                return _launchDataFlowTemplate(authClient, o);
            });
            return Promise.all(dataJobs);
        })
        .then((responses) => {
            console.log(responses);
            return res.status(200).send(responses);
        })
        .catch(err => {
            console.error('Error: ', err);
            return res.status(500).send(err);
        });

    return workflow;
});

_authenticateWithGoogle返回我等待的客户端凭据,然后再将其传递给每个返回Promise的_launchDataFlowTemplate()

我曾经认为,在链中返回Promises.all()可以通过以下.then()

解决

我想念什么?

数据流作业启动器:

function _launchDataFlowTemplate(authClient, dataJob) {

    // code snipped

    const dataflow = google.dataflow({
        version: 'v1b3',
        auth: authClient
    });

    return new Promise((resolve, reject) => {
        dataflow.projects.templates.launch({
                // template resource built from dataJob
            }
        }, (err, response) => {
            if (err) {
                console.error('Dataflow template launch failed because of ', err);
                reject(err);
            } else {
                resolve(response.data.job);
            }
        });
    });
}

身份验证:

function _authenticateWithGoogle(scopes) {
    console.log("Authenticating...");

    scopes = scopes || [
        'https://www.googleapis.com/auth/cloud-platform',
        'https://www.googleapis.com/auth/compute',
        'https://www.googleapis.com/auth/compute.readonly',
        'https://www.googleapis.com/auth/userinfo.email'
    ];

    return new Promise((resolve, reject) => {
        google.auth.getApplicationDefault((err, authClient, projectId) => {
            if (err) {
                console.error('Authentication failed because of ', err);
                reject(err);
            }

            if (authClient.createScopedRequired && authClient.createScopedRequired()) {
                authClient = authClient.createScoped(scopes);
                authClient.projectId = projectId;
            }

            console.log('Authenticated.');
            resolve(authClient);
        });
    });
}

0 个答案:

没有答案