Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active December 24, 2020 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/c0cfa9e57243672101b9e2587037911a to your computer and use it in GitHub Desktop.
Save DavidWells/c0cfa9e57243672101b9e2587037911a to your computer and use it in GitHub Desktop.
Force Async function to be sync for react server component. From https://davidwells.io/snippets/forcing-async-functions-to-sync-in-node
const forceSync = require('sync-rpc')
const syncFunction = forceSync(require.resolve('./async-thing'))
console.log('before')
const paramOne = 'foo'
const paramTwo = 'bar'
const syncReturnValue = syncFunction(paramOne, paramTwo)
console.log('syncReturn', syncReturnValue)
console.log('after')
const AWS = require('aws-sdk')
const dynamoDB = new AWS.DynamoDB({
region: 'us-east-1'
})
async function doThing() {
return (paramOne, paramTwo) => {
// Your async code here
return dynamoDB.describeTable({ TableName: 'blog-posts' }).promise().then((x) => {
return x
})
}
}
module.exports = doThing
{
"name": "force-syncer",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"aws-sdk": "^2.817.0",
"sync-rpc": "^1.3.6"
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment