SPFx example with Promises, What does TypeScript do with this code?

Iron Contributor

Still trying to learn SPFx...








What does TypeScript add to this code when transpiling? 


I do not understand why (all over, in this Todo example) Promises are created and (line 101) immediatly resolved.



2 Replies

Hi Danny,


This data service introduces the logic, flow, which isn't wired with the "real" data.

Data service operates with a locally stored (in the memory) ITodo[] array, without any physical async requests to SharePoint lists, for instance.

Yet, with such implementation, it's possible to change only the DataService by adding async operations and move resolve() into another promise or a callback, with the other part of the application untouched.

So, no matter that in the example, it can be senseless to introduce promises inside sync piece of code due to its unnecessity, as it might have seen from a first glance, it actually introduces an architecture which can be painlessly extended with async communication with a real server. 


Sorry if I understood your question wrong and wrote an obvious explanation of the code example.



Best regards,


This code does:


1. Create  Promise

2. Loop Items

3. resolve a Promise

4. return Promise


even if no "real" data is wired I do not understand this example


A normal Promise should look like


1. Create Promise AND declare function

        A. Loop items

        B. resolve Promise

2. return Promise



So why is there a Promise in this SPFX example?


The only reason I could think of is that it is not incorrect code, and TypeScript makes it work.