vuejs / vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
http://v2.vuejs.org
MIT License
208k stars 33.69k forks source link

Support promise for renderToString in vue-server-renderer #6160

Closed maunier closed 7 years ago

maunier commented 7 years ago

What problem does this feature solve?

Because now the render.renderToString function return nothing, but its a async function, i need return response in its callback, it will lead to bug in koa. I have to wrap it to a function return promise to let it to work. Something like this:

function render(renderer) {
    let resolve;
    const promise = new Promise(r => resolve = r);

    renderer.renderToString(context, (err, html) => {
      resolve(html);
    });

    return promise;
}

server.get('*', async ctx => {
    const res = await render(renderer);
    ctx.status = 200;
    ctx.body = res;
})

Maybe the render.renderToString return a promise will be better ?

What does the proposed API look like?

koa2:

server.get('*', async ctx => {
    const res = await renderer.renderToString(context);
    ctx.status = 200;
    ctx.body = res;
})
JounQin commented 7 years ago

Maybe you can use util.promisify.

nickmessing commented 7 years ago

I do like that, considering we have somehow similar API with Vue.nextTick where you can pass callback or you get a promise.