My javascript skill are a bit dated, so I want to consult your for a best practice advice.
What I want to achieve:
- Create a class that wraps around a JSON fetched from an external service
- Create another class that acts as a factory for former class encapsulating all network stuff
- Understand how Promises are handled in this
Here's a draft:
class DataObject {
constructor(json) {...}
someMethod() {...}
}
class DataObjectFactory {
constructor() {...}
getDataObject(id) {
..fetch(url)..
}
}
myDataObjectFactory.getDataObject(123).then(...)
Now, I could force getDataObject()
for act synchonously but instead i want to understand what would be the best way to make use of Promises in this case. How do I have to organize and write my stuff in order to ensure that DataObject is successfully populated with the JSON data?
Links to useful howtos also welcome.
Thank you!