最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Promise has "Variable declaration expected." error - Stack Overflow

programmeradmin4浏览0评论

I'm a new user of Promises in JavaScript and I have the following code snippet:

const arrayToTest;
let c = seasonalityService.getAnalysis(site, key)
    .then(function (result) {
        let date = moment();
        console.log(result.heating.getSeasonMonths(date));
        arrayToTest = result.heating.getSeasonMonths(date);
        console.log(result.cooling.getSeasonMonths(date));    

    })
    , function (error) {
        console.error('an error occured!!!', error);
    };

I have aService.js where I have getAnalysis method:

aService.getAnalysis = function (site, key) {
    return Promise.all([
        aService.heat(site, key),
        aService.cool(site, key)
    ]).spread(function (heating, cooling) {
        return { heating: heating, cooling: cooling };
    });
};

I tested the first part in similar situation and it worked fine but now before I pile the code if I hover over function (error) it says [js] Variable declaration expected.

I don't understand what variable does it need.

I'm a new user of Promises in JavaScript and I have the following code snippet:

const arrayToTest;
let c = seasonalityService.getAnalysis(site, key)
    .then(function (result) {
        let date = moment();
        console.log(result.heating.getSeasonMonths(date));
        arrayToTest = result.heating.getSeasonMonths(date);
        console.log(result.cooling.getSeasonMonths(date));    

    })
    , function (error) {
        console.error('an error occured!!!', error);
    };

I have aService.js where I have getAnalysis method:

aService.getAnalysis = function (site, key) {
    return Promise.all([
        aService.heat(site, key),
        aService.cool(site, key)
    ]).spread(function (heating, cooling) {
        return { heating: heating, cooling: cooling };
    });
};

I tested the first part in similar situation and it worked fine but now before I pile the code if I hover over function (error) it says [js] Variable declaration expected.

I don't understand what variable does it need.

Share Improve this question edited Apr 21, 2017 at 12:32 Samurai Jack asked Apr 20, 2017 at 9:17 Samurai JackSamurai Jack 3,1359 gold badges36 silver badges59 bronze badges 3
  • voted to close because it's a simply typographical error – Alnitak Commented Apr 20, 2017 at 9:28
  • 1 }) -> } and }; -> }); – Jaromanda X Commented Apr 20, 2017 at 9:53
  • 1 const arrayToTest; without an initialiser expression is a syntax error. – Bergi Commented Apr 21, 2017 at 12:53
Add a ment  | 

2 Answers 2

Reset to default 3

The message appears to relate to const arrayToTest;, which is illegal.

A const must be initialised at the point of declaration.

Read about const here.

You have

})
    , function (error) {

instead of

}
    , function (error) {
发布评论

评论列表(0)

  1. 暂无评论