Angularjs $ http promise object – How it works?

All, I’m a beginner to Angular framework and have been reading $http service. I thought I understood Promise object in Angular JS until the below questions popped up my mind. Can you please help me understand?

– When I make a REST call from Angular, Angular work not make the rest call before it executes the other steps in the js from where the REST API is invoked. This is because it’s single threaded. Okay I get this. So when does it execute the REST call? May be after all the instructions in the current JS are done? If yes,

  • Why do we even call it asynchronous?
  • Secondly, during the time it executes the REST API (let’s say the REST API results in an output not before 2 seconds), right after it calls REST API would Angular just wait for the 2 seconds without doing anything?

If $http service executes the REST API asynchronously as soon as it sees it,

  • Who spawns the second thread to execute the REST API? The framework? If so then are they making Javascript no longer be uni–threaded?
  • Secondly when the service returns while the main thread hasn’t even completed executing all the lines in my js, does Angular/jQuery goes and executes the instructions coded in the .then function, leaving the instructions in the main thread waiting?

Thanks much for your help!

Prem

First of all, it wont block the execution, it sends the request immediately. Basically it follow the promise pattern which uses the concept of callback function. So whenever API returns the response it calls the callback function and resume the process.

There is no concept like second thread. When HTTP return the response the execution doesn’t stop. it just call its callback method and after that excute the remaining line of code

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Angularjs $ http promise object – How it works?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录