'NodeJS async class with singleton design pattern
Problem: Implement the singleton design pattern with async calls in the class. I can't get the values when i import the module into a second file.
Case: Eventually i want to implement this logic for a bearer token update when it expires. I have multiple functions that relies on the token. So when i get a 403 i will update the token and all the other function have acces to this updated token as well.
Script 1:
//File1.js
import axios from "axios";
async function getJson(n) {
const req = await axios.get(
`https://jsonplaceholder.typicode.com/todos/${n}`
);
return req.data.title;
}
class Todo {
constructor() {
if (Todo.instance == null) {
this.title;
Todo.instance = this;
}
return Todo.instance;
}
async init() {
this.title = await getJson(1);
}
async updateTodo(n) {
this.title = await getJson(n);
}
getTodo() {
return this.title;
}
}
const todo = await new Todo().init();
export default todo;
Script 2:
const todo = await import('./File1.js');
console.log(todo); //[Module: null prototype] { default: [AsyncFunction (anonymous)] }
todo.updateTodo(3) //TypeError: todo.updateTodo is not a function
Script 2.1:
import todo from './File1.js';
await todo.init();
console.log(todo.getTodo())
await todo.updateTodo(2)
console.log(todo.getTodo())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
