Thursday, 25 May 2017

The promise doesn't resovles when put the createReadStream action out of mocha's it() function

const post = function(form_data)
{
    return new Promise(function(resolve, reject)
    {
        request.post
        (
            {
                url: 'http://localhost:40001',
                formData: form_data
            },
            function(err, res, body)
            {
                if(err)
                {
                    console.log(err);
                    resolve(err);
                }
                else
                {
                    console.log(body);
                    body = JSON.parse(body);
                    if(body.status !== 'ok')
                    {
                        resolve(body);
                    }
                    else
                    {
                        resolve(body);
                    }
                }
            }
        );
    });
};

const form_data = {};

The problems begins from here !

Note: all the code below is in describe() block.

This fails: the promise does not resolve

form_data.file = fs.createReadStream(path.resolve(config.sample_dir.ppt, 'test.ppt'));
it('should not accept the form having only the file to transform', function()
{
    const result = post(form_data);
    result.then(function()
    {
        console.log('resolved');
    });
    return result;
});

This passes: the promise is resolved

it('should not accept the form having only the file to transform', function()
{
    form_data.file = fs.createReadStream(path.resolve(config.sample_dir.ppt, 'test.ppt'));
    const result = post(form_data);
    result.then(function()
    {
        console.log('resolved');
    });
    return result;
});

Where I get confused is why it works after the createReadStream is moved into it(),and why the promise do not resolve if createReadStream is out of it().

Anyone can help ? Thank you very much !



via twesix

No comments:

Post a Comment