plain-car-17885
05/14/2024, 8:40 PMjavascript
const API_KEY_HTMLDOCS = '...'
axios
.post(
'https://api.htmldocs.com/v1/api/generate',
{
projectId: '...',
path: 'index.html',
context: {
business: 'test'
}
},
{
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + API_KEY_HTMLDOCS
}
}
)
.then((response) => {
console.log(response.data.url)
workflow.pdf_url = response.data.url
})
.catch((error) => {
console.log(error)
})
bumpy-butcher-41910
05/14/2024, 8:43 PMbumpy-butcher-41910
05/14/2024, 8:43 PMplain-car-17885
05/14/2024, 8:45 PMplain-car-17885
05/14/2024, 8:46 PMbumpy-butcher-41910
05/14/2024, 8:52 PMplain-car-17885
05/14/2024, 8:52 PMplain-car-17885
05/14/2024, 8:56 PMjavascript
console.log('Start')
setTimeout(function () {
console.log('Waited for 5 seconds')
workflow.new_question = 'Test'
}, 5000) // 5000 milliseconds = 5 seconds
new_question will not be changed in this code, but the log prints correctlyplain-car-17885
05/14/2024, 9:01 PMjavascript
console.log('Start')
setTimeout(function () {
console.log('Waited for 5 seconds')
workflow.new_question = 'Test'
console.log('workflow.new_question after timeout:', workflow.new_question)
}, 5000)
the second log prints "workflow.new....: Test".
So the variable is changing locally in the code but not for the bot/workflow. Very weird..bumpy-butcher-41910
05/14/2024, 9:02 PMplain-car-17885
05/14/2024, 9:05 PMnice-airplane-37559
05/14/2024, 9:38 PMnice-airplane-37559
05/14/2024, 9:38 PMnice-airplane-37559
05/14/2024, 11:28 PMnice-airplane-37559
05/14/2024, 11:29 PMnice-airplane-37559
05/14/2024, 11:33 PMplain-car-17885
05/15/2024, 8:13 AMjavascript
const API_KEY_HTMLDOCS = '...'
const generatePDF = async () => {
try {
const response = await axios.post(
'https://api.htmldocs.com/v1/api/generate',
{
projectId: '...',
path: 'index.html',
context: {
business: 'test'
}
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${API_KEY_HTMLDOCS}`
}
}
)
console.log(response.data.url)
workflow.pdf_url = response.data.url
} catch (error) {
console.error(error)
}
}
generatePDF()
`
plain-car-17885
05/15/2024, 8:14 AMplain-car-17885
05/15/2024, 8:14 AMnice-airplane-37559
05/15/2024, 8:19 AM.then()
and .catch()
methods**:
After sending a request, you can use these methods to handle the response and errors respectively. The code execution inside these methods will only run after the Axios promise resolves, ensuring that you have the data you need before it executes.
javascript
axios.get('https://example.com')
.then(response => {
console.log('Data:', response.data);
})
.catch(error => {
console.error('Error:', error);
});
2. **Using async/await**:
This is a more modern syntax to handle promises. You can mark a function as async
, which allows you to use await
to pause the function’s execution until the promise resolves. This makes your asynchronous code look and behave a little more like synchronous code, which can be easier to understand and manage.
javascript
async function fetchData() {
try {
const response = await axios.get('https://example.com');
console.log('Data:', response.data);
} catch (error) {
console.error('Error:', error);
}
}
fetchData();
In both cases, the code execution "waits" at the point of the request until the server responds, and then it proceeds with the returned data or handles any errors. This waiting is crucial for working with any asynchronous API calls in JavaScript.plain-car-17885
05/15/2024, 8:30 AMnice-airplane-37559
05/15/2024, 8:32 AMplain-car-17885
05/15/2024, 8:33 AMnice-airplane-37559
05/15/2024, 8:34 AMnice-airplane-37559
05/15/2024, 8:36 AMplain-car-17885
05/15/2024, 8:36 AMplain-car-17885
05/15/2024, 8:36 AMplain-car-17885
05/15/2024, 8:43 AMnice-airplane-37559
05/15/2024, 9:03 PMplain-car-17885
05/16/2024, 5:18 PM